Re:Re:Folder Actions
Re:Re:Folder Actions
- Subject: Re:Re:Folder Actions
- From: "Kinsella, John R." <email@hidden>
- Date: Wed, 18 Sep 2002 18:58:02 -0500
Nick,
Wow! That was more than I could have ever hoped for, thanks a ton for your
work. Both folder actions and repeat loops that perform actions on a list
of files are very new and unknown to me. Your help below has really opened
my eyes to what was wrong with the way I was thinking about (and scripting)
this process. I just can't say thank you enough!
John Kinsella
IRT-CS
University of St. Thomas
-------------
"The surest way to corrupt a youth is to teach him to hold in higher regard
those who think alike rather than those who think differently."
--Nietzsche
--__--__--
Message: 5
Date: Wed, 18 Sep 2002 11:29:54 +0100
Subject: Re: Folder Actions
From: Mr Tea <email@hidden>
To: AS List <email@hidden>
This from Kinsella, John R. - dated 17-9-02 10.18 pm:
>
Can someone PLEASE help me figure out what I'm doing wrong with this
folder
>
action.
Er, well, I'll give it a go. As a long-time champion of folder actions, I
can do no less.
The first thing I noticed when I checked the syntax of your script was that
Microsoft Word didn't understand the 'select all' command. The word 'all'
appeared as a variable in the script, so I looked in the MS Word dictionary,
and found that 'set selection to' is what's required. And you need to direct
MS Word to the specific element (the text) of a specific window (document 1)
in order to get a useable result.
Also, there seemed to be a lot of stuff in the script that didn't do
anything at all, at least, nothing related to what you wanted to achieve. I
cleared out some of the dead wood (including the unnecessary use of the
clipboard to get text out of and into Word), and applied a monkey wrench to
what remained.
To help me figure out why the script wasn't working, I wrapped it in a try
block so that I could see any error messages that were being generated. The
first one I got was telling me that Word couldn't continue with the
'SearchReplace' command. I tried putting 'my' in front of the line to show
Word that it needn't concern itself with this part of the script, and
waddaya know! It worked! The next error I encountered was that the main
script didn't understand the variable 'newText' set in the SearchReplace
handler. So I declared newText as a global variable at the top of the
script, and again, it worked! In fact, the whole script worked, and here it
is:
global newText
on adding folder items to this_folder after receiving added_items
set the folder_name to name of (info for this_folder)
try
repeat with this_item in added_items
set openItem to this_item as alias
tell application "Microsoft Word"
activate
open openItem
repeat until name of document 1 = openItem as string
delay 1
end repeat
copy text of document 1 as text to ourText
set findIt to "pineapple"
set replaceItWith to "cranberry"
my searchReplace(ourText, findIt, replaceItWith)
set text of document 1 to newText
save document 1
close document 1
end tell
end repeat
on error errmsg
display dialog errmsg
end try
end adding folder items to
on searchReplace(theText, SearchString, ReplaceString)
set OldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to SearchString
set newText to text items of theText
set AppleScript's text item delimiters to ReplaceString
set newText to newText as text
set AppleScript's text item delimiters to OldDelims
end searchReplace
I hope this does what you need, and that my description of the debugging
process has been useful. I'm not putting it forward as an example of 'good
practice', it just shows the procedures used by this moderately experience
AS user in order to achieve a functional script.
If you have any questions about other changes in the script, don't hesitate
to ask, and I'll try to come up with an explanation that goes beyond 'it
seemed like it might work'. And I'm sure that others on this list will be
able to weigh in with further refinements...
What I don't understand is why the script kept returning me to the Finder
after each iteration of the main repeat loop. Is this a 'feature' of Folder
Actions in OS X?
Regards
Nick
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.