So here’s the scenario:
I am nearly brand new when it comes to AppleScript. I have a scant week of experience under my belt. I started learning because I need an automatic and self-sorting archival system for my photography/vieography business. After a few days of reading and tinkering, I was able to put a script together on my laptop (running version 10.6.8) and it works exactly as I had hoped it would. I’m very proud of this fact considering the 3 days of knowledge I had before writing it.
The problem I’m having now is that I now can’t seem to get the same script working on my iMac workstation (I wrote it on the laptop so I could spend more time learning/working on it). At first I tried just moving the .scpt file over via USB drive and plugging in the appropriate folder paths where needed. That yielded nothing. So I am re-building the script on the iMac (10.7.4). So far everything is working except the part where I parse the filenames (and one other part that I’ll get to towards the end of this post).
The naming convention of my project folders is “year_month_day_clientName_projectName”
(Example “2012_08_03_myClient_projectName”
The script is supposed to use “_” to separate out each “chunk” of the folder name, so that I can call on item 4 of that list (the client name) to use in the sorting if block immediately after in the script. On the laptop I used to originally write the script, it works perfectly. When I try it on the iMac however, it grabs the 4th character (the 2nd “2” from “2012”) instead of grabbing the whole client name from the list of parsed chunks. I have tried everything I can think of to get it to grab the whole client name portion of the folder name, but I can only get it to grab the last character of the year. Am I doing something wrong? Does AppleScript’s text item delimiters behave differently for Lion? I could really use the help of someone more experienced.
Here’s the script in entirety:
on adding folder items to this_folder after receiving these_items
tell application "Finder"
set sortList to (every folder in this_folder)
set sortItems to {}
repeat with i in sortList
set end of sortItems to (name of i)
end repeat
set newLocation to ((POSIX file "/Volumes/Drive1/Archive") as alias)
repeat with p in sortList
set clientArchive to (every folder in ((POSIX file "/Volumes/Drive1/Archive") as alias))
set clientList to {}
repeat with c in clientArchive
set end of clientList to (name of c)
end repeat
set clientChoice to item 1 of (choose from list clientList with prompt "Choose for " & p)
set p_Name to (name of p) as string
set AppleScript's text item delimiters to "_" --set the delim
set p_Parsed to every text item in p_Name
set AppleScript's text item delimiters to "" --reset the delim
if clientChoice = ("**New Client**") then
--makes a new folder with the right client name
set newFolder to (make new folder at newLocation with properties {name:item 4 of p_Parsed})
move p to newFolder
else
--places the folder in the chosen client archive folder
set targetClient to (folder (clientChoice) in newLocation)
move p to targetClient
end if
end repeat
end tell
end adding folder items to
To help narrow it down as far as I can, the script IS in the appropriate folder action scripts folder in the library, and every other part of the script seems to be working. When I choose an existing client folder, the folder in question is sorted properly. When I choose to make a new folder, a new folder called “2” is made. So as near as I can tell, the only thing causing problems is the section regarding text item delimiters.
The second problem I am having is that the script seems to be copying the items I want to sort instead of moving them, despite the fact that I am using the “move” command. What gives? The whole idea of this archive system is that it will keep my entire archives off of my workstation and onto and external drive where it can’t eat up space. There’s a 2nd drive I will be setting up to mirror the 1st as added safety once I get this script working. I’m less concerned about this problem though, because in a worst-case scenario I can have the script send those extra remainders to the trash bin. (Of course I would be more pleased if I could just have it behave like it should in the first place
)
Any help would be greatly appreciated!
(PS: I know this is likely to be bulkier than it needs to be, considering this is the first script I have written on my own that wasn’t just some tutorial exercise. I’d gladly welcome any tips or suggestions to help condense it, but obviously my primary concern is sorting out this parsing problem)