Hi!
I’m the new guy!
I haven’t been using Applescript (or any other programming language, for that matter) for very long, so have mercy on me.
Here’s what I’m trying to accomplish:
Create a folder in a given place, named with the current date and an incremental number at the back, e.g. DDMMYY_1
In case the folder already exists, i want to increment the number and create the corresponding folder (DDMMYY_2, DDMMYY_3, and so on .)
I want to be able to do this without any restrictions, number-wise (so DDMMYY_123456789 should be valid)
Here’s what I’ve come up with so far:
set date to do shell script "date +'%d%m%y'"
set folderpath to ":Volumes:Arbeitsdaten:Konrad:Bildersuchen:" as alias
set x to 1
set datefolder to setDatefolder(x, date)
-- display dialog ordnerpfad as text
tell application "Finder"
repeat while (exists folder datefolder of folderpath)
set x to x + 1
set datefolder to setDatefolder(x, datum)
end repeat
make folder at folderpath with properties {name:datefolder}
end tell
on setDatefolder(counter, date)
set dateTemp to date & "_" & counter as text
return dateTemp
end setDatefolder
I’ve changed the variables’ names from German to English to make the script easier to read for you guys.
As you can see, nothing too fancy here, check for the folder name, if it exists, increment, create a new folder name, and so on 'til a folder name is checked FALSE. Then create a folder with this name.
The whole thing works fine the first time, but once I want to create the second folder, I get an error:
, which roughly means Finder has encountered an error: can’t continue:setDatefolder.
I feel really stupid, I’ve been screwing around with this tiny script all morning and can’t get it to work. Could someone please point out the obvious to someone who doesn’t see the wood for the trees (does that saying even work in English?).
Thanks in advance.
Greetings from arctic Germany
Konrad
Model: iMac 24", 2,8Ghz
AppleScript: 2.0.1
Browser: Safari 525.26.12
Operating System: Mac OS X (10.5)
if a handler is called within an application tell block, the keyword my has to be added
because a handler belongs to AppleScript itself, not to the application
set datefolder to my setDatefolder(x, datum)
PS: the translation from Datum to date in the first line is awkward, because date is a reserved word
There’s no need to call a handler for 1 line of code.
set mydate to do shell script "date +'%d%m%y'"
set folderpath to ":Volumes:Arbeitsdaten:Konrad:Bildersuchen:" as alias
set x to 1
set datefolder to mydate & "_" & x as text
tell application "Finder"
repeat while (exists folder datefolder of folderpath)
set x to x + 1
set datefolder to mydate & "_" & x as text
end repeat
make folder at folderpath with properties {name:datefolder}
end tell
Note that I’ve changed ‘date’ to ‘mydate’ in order to avoid the keyword problem.
AWESOMENESS!! It works! Thanks for the turbo-fast response.
Believe it or not, this is the first time I’ve come across this “my” thing, gonna have to read up on this. So, without the “my” the script was thinking, an application-specific handler was supposed to get called? Or a application-specific sub-routine? Gotta get reading .
@StefanK: Mmmh, didn’t think about the keyword-problem. Applescript really is pretty close to the English language, ain’t it?
@cwtnospam: I know, the handler seems a bit overkill for what it does, but this script is ONLY THE BEGINNING! evillaugh I’m going to play around with different naming schemes and don’t want to have to change my code in several places everytime I make a change. But if the script was only to provide the functionality it does right now I’d be with you on the subject.
And just because it says in the forum guidelines to do this, here’s the (now working) script in all its beauty:
set datum to do shell script "date +'%d%m%y'"
set ordnerpfad to ":Volumes:Arbeitsdaten:Konrad:Bildersuchen:" as alias
set x to 1
set datumsordner to setDatumsordner(x, datum)
-- display dialog ordnerpfad as text
tell application "Finder"
repeat while (exists folder datumsordner of ordnerpfad)
set x to x + 1
set datumsordner to my setDatumsordner(x, datum)
end repeat
make folder at ordnerpfad with properties {name:datumsordner}
end tell
on setDatumsordner(zaehler, datum)
set datumTemp to datum & "_" & zaehler as text
return datumTemp
end setDatumsordner
If you’re likely to build up a large collection of folders this way, you may find it faster to get all the existing names in one go and test each new name textually against these, rather than doing individual ‘exists’ tests against the items on disk:
tell application "Finder" to set currentNames to name of every item of ordnerpfad
repeat while (datumsordner is in currentNames)
set x to x + 1
set datumsordner to my setDatumsordner(x, datum)
end repeat
tell application "Finder" to make new folder at ordnerpfad with properties {name:datumsordner}
Another way might be to find the largest numbered folder and add 1:
set x to 0
set max to 0
set ordnerpfad to choose folder
tell application "Finder"
set currentNames to name of every folder of ordnerpfad
set tid to AppleScript's text item delimiters
repeat with eachname in currentNames
set AppleScript's text item delimiters to {"."}
set num to text item 1 of eachname
set AppleScript's text item delimiters to {"_"}
set num to text item 2 of num
if num > max then
set max to num
end if
end repeat
set AppleScript's text item delimiters to tid
end tell
display dialog max
set max to max + 1
@Nigel: Would this be more efficient if I had lots of folders? I would imagine so, since by gut-feeling it should be less work for the script to only compare strings instead of checking for the existence of a folder. But is it really so?
@cwtnospam: That’s actually what I was trying to accomplish. I’m going to use your example to read up on text delimiters, hadn’t really used those, yet. Quick question: isn’t the script you posted used for checking filenames instead of folder names? Seems like it’s extracting the part before a ˜.’ and then checks the extracted bit for the number behind a ˜_', or am I mistaken?
Thanks to the both of you for the additional information, very helpful examples and good pointers for my further ventures into Applescript. Most times, it’s very useful to get a pointer in a direction, so one can focus ones efforts instead of just jumping headfirst into the vast pool of information called “The Internet”.
I think, I feel a little addiction coming up (“There MUST be a way to do this with a script!”), anyone else experience this?
Yes. Working with text is much faster than working with items on disk. Obviously the names have to be got from the disk first, but getting them all with one command is much faster than repeatedly going back to the disk to check if a folder with a certain name exists.
cwtnospam’s suggestion is possibly faster still and has the advantage of incrementing the highest number rather than just filling the first available vacancy (which isn’t be the same thing if any of the previous folders have been deleted). However, there’s a vulnerability at the moment in that the numbers in the folder names are text and there’s no provision for leading zeros, so “num > max” is a lexicographical comparison rather than a numerical one. (eg. “2” > “100” = true.) The numeric texts should either be coerced to number first or, if you’re using Tiger or later, compared “considering numeric strings”.
I’m not clear if you want an incremental number range to cover all the folders or separate ranges for folders with equal dates.
set datum to do shell script "date +'%d%m%y'"
set ordnerpfad to ":Volumes:Arbeitsdaten:Konrad:Bildersuchen:" as alias
set x to 0
set max to 0
set ordnerpfad to choose folder
tell application "Finder" to set currentNames to name of every folder of ordnerpfad
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"_"}
repeat with eachname in currentNames
-- if (eachname begins with datum) then -- Uncomment if you only want to consider folders with the current date.
set num to (text item -1 of eachname) as number
if (num > max) then set max to num
-- end if
end repeat
set AppleScript's text item delimiters to tid
display dialog max
set max to max + 1
set datumsordner to setDatumsordner(max, datum)
tell application "Finder" to make new folder at ordnerpfad with properties {name:datumsordner}
on setDatumsordner(zaehler, datum)
set datumTemp to datum & "_" & zaehler as text
return datumTemp
end setDatumsordner
With AppleScript, it’s: “There must be MANY ways to do this with a script. Which is the best under the current circumstances?” But even second-best can be immensely satisfying when you get it working.