I have had some experience programming but have only just started using AppleScript. Was hoping someone might be able to answer my question…
I have written a script that unzips each rar file I put into a folder by opening it (in default unarchiver):
on adding folder items to this_folder after receiving these_items
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
set the item_info to info for this_item
if the name extension of the item_info is in {"rar"} then
tell application "Finder"
open this_item
end tell
end if
end repeat
end adding folder items to
However, often I am copying in folders, sometimes with subfolders that contain the rar files. Is there an easy way to modify this code so that it will look for files in any folder and subfolders added to the target folder. Perhaps so that in the above code these_items would include all files contained in the folder and subfolders?
Hi,
a folder action script affects only the folder which it is attached to,
watching subfolders is not possible unless you attach the script to each subfolder
Thanks…
If the item copied in is a folder, could I write code that goes through each item of the folder?
This is a (not tested) piece of code that recursively goes thru every item of any folder to see if their are RARs. If the script detects a RAR, it moves it to a folder name after the current date, in a ‘processed folder’ and opens the moved rar.
property processedFolderName : "Processed RARs"
global processedFolder
on adding folder items to theFolder after receiving theObjects
folderPOSIX to (POSIX path of theFolder) as string
if folderPOSIX does not end with "/" then set folderPOSIX to (folderPOSIX & "/") as string
set processedPosix to (fodlderPOSIX & processedFolderName)
set processedFolder to mkdir(processedPosix)
tell application "Finder" to set allItems to every item of theFolder whose name is not processedFolderName
mainRecursive(allItems)
end adding folder items to
on mkdir(aPOSIX)
try
do shell script "mkdir " & quoted form of aPOSIX
end try
try
set newAlias to (POSIX file aPOSIX) as alias
return newAlias
on error
return missing value
end try
end mkdir
on mainRecursive(startObjects)
repeat with i in startObjects
set i to i as alias
if folder of (info for i) is true then -- if folder do recursive
tell application "Finder" to set newObjects to every item of i
mainRecursive(newObjects)
else if name extension of (info for i) is "rar" then -- if rar
set curDate to (date string of (current date)) as string
set fileName to name of (info for i)
set dateFolder to (POSIX path of processedFolder)
if dateFolder does not end with "/" then set dateFolder to (dateFolder & "/") as string
set dateFolder to (dateFolder & curDate) as string
set dateFolder to mkdir(dateFolder) -- folder for today
tell application "Finder" to move i to dateFolder -- move
tell application "Finder" to set movedFile to (file fileName of dateFolder) as alias -- get moved item
do shell script "open " & quoted form of (POSIX path of movedFile) -- open moved item
end if
end repeat -- repeat for all objects
end mainRecursive
It’s not tested so it could be that’s it not working,
ief2
Thanks so much for that - it got me there in the end and everything is tested and working now!
One question I had - something which seemed to be key to making everything work was the code I found in the above script ‘set i to i as alias’
What did this do?
When you request items of the Finder, it returns the reference in a special way (eg: file “A Name” of folder “Desktop” of folder “username” of folder “Users” of disk “Macintosh HD”). If you want to use this reference outside a Finder tell block, you first have to convert them to aliases.
Ief2
Thanks very much for your help - much appreciated