PDA

View Full Version : AppleScript to search for folder name




jtcdesigns
Apr 2, 2012, 11:51 AM
I'm pretty new to AppleScript but not new to programming. I've recently come across a project where I think scripting will be the best idea.

A user puts files in a folder and puts the job number as the name of the folder. They drop this folder into a release folder.. this then triggers the script to detect the job number and move that folder to an archive folder.

Example: user drops folder "103945" into release folder. The script detects that it starts with "103" and moves it into the archive folder "103000"

I hope its possible to do, It would be good if the script can detect the first 3 digits to the job number and then search archives for the folder starting with the same first 3 digits. Then move that folder into the folder on archives. And if possible.. if the folder doesn't exist then it creates a folder by the first 3 digits of the job number plus 000



kryten2
Apr 2, 2012, 04:45 PM
The script wil move the folders like you wanted. The only thing still missing
is this part "if the folder doesn't exist then it creates a folder by the first 3 digits
of the job number plus 000". Got something else to do right now.
Will get back when I finish my other work. This is a droplet not a folder action. Script has some errors, sorry for that.

global FolderName
global this_folder
global archive_item_info
-- This droplet processes folders dropped onto the applet
on open these_items
repeat with i from 1 to the count of these_items
set this_item to item i of these_items
set the item_info to info for this_item
copy displayed name of item_info to FolderName
-- set FolderName to text 1 thru 3 of FolderName
if (alias of the item_info is false) and (folder of the item_info is true) then
process_item(this_item)
end if
end repeat
end open

-- this sub-routine processes folders
on process_item(this_item)
-- NOTE that the variable this_item is a folder reference in alias format
-- FOLDER PROCESSING STATEMENTS GOES HERE
--CHANGE THE LINE BELOW TO YOUR ARCHIVES FOLDER !
set archivesFolder to (path to desktop as text) & "Archives" as alias
-- set archivesFolderList to list folder archivesFolder
tell application "Finder"
set archivesFolderListAlias to (entire contents of (archivesFolder) as alias list)
end tell
repeat with aFolder from 1 to the count of archivesFolderListAlias
set this_folder to item aFolder of archivesFolderListAlias
set archive_item_info to info for this_folder
copy displayed name of archive_item_info to archiveFolderName
if archiveFolderName contains text 1 thru 3 of FolderName then
tell application "Finder"
move this_item to this_folder
end tell
end if
end repeat
end process_item

jtcdesigns
Apr 3, 2012, 07:22 AM
I appreciate the help. I'll look over this with my coworker and see if we can iron out the errors.

jtcdesigns
Apr 3, 2012, 08:50 AM
would there be a way a windows user could use a droplet? I probably should have posted that. We use an automation program called FullSwitch. What I can do in that is setup some canned rules that say if job name begins with "103" then it goes to folder "103000". The only problem is I'll have to create those folders ahead of time. That is why I thought going to a script may be better because it will detect the name and put it in its right folder. So I basically just need a script that I can have FullSwitch run everytime a folder is placed in a folder.

I like the droplet idea, I think I may actually use that for use at home with my photography.. everytime I get a new project I have to copy the files over to a certain location.. I could just make it automatic :D

I may have to for now just make a bunch of folders ahead of time so users can just drop in the folders into one location that switch monitors until I can get a script to automate the task.

kryten2
Apr 3, 2012, 05:30 PM
My apologies once again for the provided script. I made it very quickly without
thoroughly testing it. I don't know if a window user could use a droplet or not.
Making a bunch of folders on a mac should only take a few seconds for example :
mkdir {1..999}000 will make folders 1000 to 999000 in a flash.
You can try posting your problem on http://stackoverflow.com/questions/tagged/applescript
The guys there really know their stuff and should provide you with a working
script in no time.

kryten2
Apr 4, 2012, 09:18 AM
As usual I made things more complicated then they were with my first attempt. Two things needed for this. First thing : your release folder with this script attached to it. Second thing : An empty archives folder. Folders will be created by the first 3 digits of the job number plus 000.


on adding folder items to source_folder after receiving these_items
-- insert actions here
if folderReady(source_folder) then
--CHANGE THE LINE BELOW TO YOUR ARCHIVES FOLDER !
set archivesFolder to (path to desktop as text) & "Archives" as alias
repeat with i from 1 to number of items in these_items
set this_item to item i of these_items
-- insert actions here
set the item_info to info for this_item
copy displayed name of item_info to FolderName
set FolderNamePref to text 1 thru 3 of FolderName
set archivesFolderCheck to (archivesFolder as text) & FolderNamePref & "000" --as alias
if (alias of the item_info is false) and (folder of the item_info is true) then
tell application "Finder"
if exists archivesFolderCheck then
set archivesFolderCheck to archivesFolderCheck as alias
-- duplicate this_item to archivesFolderCheck
move this_item to archivesFolderCheck
else
set new_folder to make new folder at archivesFolder with properties {name:FolderNamePref & "000"}
-- duplicate this_item to new_folder
move this_item to new_folder
end if
end tell
else
display dialog "I only process folders" buttons {"OK"} default button 1
end if
end repeat
end if
end adding folder items to

on folderReady(tFolder)
set myFolder to tFolder as alias -- use the path to the folder that's loading
set firstSize to size of (info for myFolder) --get initial size
delay 5 --wait 5 seconds
set newSize to size of (info for myFolder) --get a newer size, bigger
repeat while newSize ≠ firstSize --if they don't equal, loop until they do
set firstSize to newSize --new base size
delay 3 --wait three seconds
set newSize to size of (info for myFolder) --get a newer size
end repeat --once the sizes match, the transfer is complete
return true
end folderReady


After a few more tests I noticed a problem. When copying 15 folders only 7 get moved. I'm on a Leopard machine and I've seen other people also on Leopard report strange and erratic behaviour with folder actions. Going to test this on a Tiger install.

I've just done a test on Tiger and the script works flawlessly. Tested with 20 folder with 500 MB of data in them. If someone else on Leopard could test this I would be grateful for the feedback.