 |
 |
Applescript help??
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: May 2001
Status:
Offline
|
|
Hello,
I'm trying to create a script to open a simpletext file in a folder. The name of the file changes everytime I download a new file from the internet. The name consist of 12 numbers and ends in the extention ".txt". If I place the script editor on record and allow it to record the series of action which I take, the script that is created works perfectly. However if the file name changes (the series of numbers in the name) the script doesn't work. I have tried changing the script so that it will open any files that end in ".txt" but I keep getting an error message that applescript can't open files with ".txt" in that folder. Is there anyway that I can write a script that will automatically open any simpletext documents in a particular folder? It does work if include the exact series of numbers in a particular file name ending with the ".txt".
Thank you,
Steph
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Apr 2001
Status:
Offline
|
|
Steph,
I guess there are a few ways to approach this, but something like this script should do it... just change the value for property theFolder ("Macintosh HD:MyDocuments:" ) to the full path to your download folder.
Code:
property file_Ext : ".txt"
property theFolder : alias "Macintosh HD:MyDocuments:" (*change to the path to your folder of choice*)
set text_List to {} -- always start with an empty list of files
set filesList to list folder theFolder without invisibles
repeat with anItem in filesList -- find all files ending in file_Ext in theFolder
set theFile to (theFolder as string) & contents of anItem
set fileInfo to info for alias theFile
if the name of the fileInfo contains file_Ext then
copy name of fileInfo to the end of text_List
end if
end repeat
if (count of text_List) > 1 then
-- if multiple files ending in file_Ext found, choose the file(s) to open
set slctd_Files to (choose from list (text_List) with prompt "Please choose a text file to open" with multiple selections allowed) as list
repeat with aFile in slctd_Files -- opens all files selected in the dialog
set filetoOpen to (theFolder & aFile) as string
tell application "Finder"
open file filetoOpen
end tell
end repeat
else -- only one file ending in file_Ext found in theFolder
set filetoOpen to (theFolder & (text_List as string)) as string
tell application "Finder"
open file filetoOpen
end tell
end if
To use with other file extensions, just change value of property file_Ext to another extension type (".doc",etc.).
By the way, if there are multiple files with the extension of choice in your folder, to automatically open all of these files (rather than choosing the ones to open in a dialog like the previous example), just a couple of changes...
Code:
property file_Ext : ".txt"
property theFolder : alias "Macintosh HD:MyDocuments:" (*change to the path to your folder of choice*)
set text_List to {} -- always start with an empty list of files
set filesList to list folder theFolder without invisibles
repeat with anItem in filesList -- find all files ending in file_Ext in theFolder
set theFile to (theFolder as string) & contents of anItem
set fileInfo to info for alias theFile
if the name of the fileInfo contains file_Ext then
copy name of fileInfo to the end of text_List
end if
end repeat
if (count of text_List) > 1 then
-- if multiple files ending in file_Ext found, just open 'em all
repeat with aFile in text_List -- opens all files found ending in file_Ext
set filetoOpen to (theFolder & aFile) as string
tell application "Finder"
open file filetoOpen
end tell
end repeat
else -- only one file ending in file_Ext found in theFolder
set filetoOpen to (theFolder & (text_List as string)) as string
tell application "Finder"
open file filetoOpen
end tell
end if
Hope this helps... Unfortunately, this may not (& probably won't) yet work in OS X, since I don't think "getting" (& opening) files from the Finder has been fully implemented.
cheers, acur
[This message has been edited by acur128 (edited 05-20-2001).]
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: May 2001
Status:
Offline
|
|
Your my hero of the day! Your script does exactly what I wanted it to do. I have been ploughing through applescipt for dummies(it's not an easy read). I don't connect with examples that the writer uses for teaching applescipt which makes it difficult to understand. I really wish the book contained more examples of how one would use these lessons in real life, sort of speak. Its full of set x to y and yada, yada, yada. I'm going to take your script and analize it with the dummies book as a reference so that I can learn why you made certain choices for this script. If you would suggest another book that has clearer examples of how one applies the scripting language for task that a beginner might you use, I would appreciate it.
Thank you again!
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |