Re: Question: Setting up a script to watch a folder in OS X?
Re: Question: Setting up a script to watch a folder in OS X?
- Subject: Re: Question: Setting up a script to watch a folder in OS X?
- From: Mr Tea <email@hidden>
- Date: Thu, 20 Jun 2002 12:39:51 +0100
This from Sven Ryen - dated 20/6/02 09.35 am:
>
A very simple question: Does anybody have examples of a script that can
>
'watch' a folder, and perform tasks when files are dropped into it, then
>
output the result to a second folder? (I guess this sounds like a job for
>
Folder Actions, but the script needs to be capable of running under OS X)
Hi, Sven. Like the other 0.007 percent of Mac users who embraced folder
actions, I'm disappointed that they haven't made it into X yet.
Their absence has prompted my first adventures with using AS 'stay-open'
applets to watch folders.
I've built a couple of working examples - one that processes sound files by
passing them on to applets adapted from Apple's own QT 5 scripts, and one
that renames exported image files and numbers them incrementally.
Both ersatz folder actions can be run either by dragging a folder onto them
or by double-clicking (works on enclosing folder), and both are configured
to open and minimise the watched folder when they start, and quit when the
folder is closed.
Here's the script that names and numbers exported image files. As always,
any suggestions for performance tweaks from the more experienced scripters
on this list are welcome.
(*START SCRIPT*)
property prevList : {}
property currList : {}
property theDelay : 3
property theFolder : ""
property theRunner : ""
property theAction : ""
property dropStart : false
property theTypes : {"PICT", "TIFF", "JPEG", "BMP "}
global theDropZone
global lastName
global theViewer
on open theDrop
set lastName to ""
set theFolder to theDrop as alias
if folder of (info for theDrop) is true then
set dropStart to true
set prevList to list folder theFolder without invisibles
tell application "Finder"
open theFolder
set collapsed of Finder window 1 to true
end tell
tell me to run
end if
end open
on run
if not dropStart then
set theRunner to path to me
tell application "Finder"
set theFolder to container of theRunner as alias
make new Finder window
set target of Finder window 1 to theFolder
set theViewer to Finder window 1
set collapsed of theViewer to true
end tell
set prevList to list folder theFolder without invisibles
set dropStart to false
end if
end run
on idle
if exists theViewer then
try
set currList to list folder theFolder without invisibles
checkFolder(theFolder)
set prevList to currList
set stayActive to true
return theDelay
on error errmsg
activate
display dialog errmsg
tell me to quit
end try
else
tell me to quit
end if
end idle
on checkFolder(theFolder)
repeat with n from 1 to count of currList
tell application "Finder" to update theFolder
if item n of currList is not in prevList and item n of currList does
not contain ".psd" and item n of currList is not lastName then
set theCandidate to ((theFolder as string) & item n of currList)
as alias
tell application "Finder"
if file type of theCandidate is in theTypes then
set theTally to count of ((files whose file type is in
theTypes and name does not contain ".psd") of theFolder)
if theTally < 10 then set theTally to " 00" & theTally
if theTally > 9 and theTally < 100 then set theTally to
" 0" & theTally
if theTally > 99 then set theTally to " " & theTally
set name of theCandidate to name of theFolder & theTally
set lastName to name of theFolder & theTally
say "Proceed"
end if
end tell
end if
end repeat
end checkFolder
on quit
continue quit
end quit
(*END SCRIPT*)
In the 'checkFolder' handler, there's a bit of extra thrashing about to get
around the temporary files that Photoshop throws up when 'saving as' and the
Finder's apparent reluctance to realise that a new item has been saved into
the folder unless you poke it with a sharp stick.
My sound processing script is very similar, except that it checks for an
alias called "[Folder Action]" in the dropped folder and uses the original
item of that alias to process eligible files (as declared in the 'theTypes'
property.
If you've got this far, I'm sure you'll have no trouble whipping up some
code to 'output the result to a second folder'.
Mr Tea
--
Brew of the day: [ibid]
_______________________________________________
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.