'm a beginner in Apple Script and I tried to compile a script from different sources in order to automate the process I need to conduct frequently on many groups (folders).
What I want I a set of sub-groups in a certain format, for instance YYYY-MM-YTD. I managed to put together the script that actually creates 12 subfolders:
2012-01-YTD
2012-02-YTD
.
.
.
2012-12-YTD
Script takes current year as default, I can change that via Dialog box and the folders are created correctly. However, that works just from Apple Script editor. When I copied script to ~/Library/Application Support/DEVONthink Pro/Scripts DEVONThink Pro hangs and I need to force quit.
Could anyone advise what am I doing wrongly?
The script:
property myMonth : {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"}
global myYear
set myYear to year of (current date)
set myYear to text returned of (display dialog "Enter year as YYYY:" default answer myYear with title "Enter year")
tell application id "com.devon-technologies.thinkpro2" to set thisSelection to the selection
tell application id "com.devon-technologies.thinkpro2"
set thisItem to first item of thisSelection
set anchorFolder to (name of thisItem) as string
set loopMyMonth to 1
repeat until loopMyMonth = 13
set monthFolder to anchorFolder & "/" & myYear & "-" & (item loopMyMonth in myMonth) & "-YTD"
set monthGroup to create location monthFolder
set loopMyMonth to loopMyMonth + 1
end repeat
end tell
I made a couple of minor structural changes, and added error checking to ensure that something is selected and that the selection is a group. Otherwise, if I save this as a compiled script, or as a compiled script bundle, in a folder in ~/Library/Application Support/DEVONthink Pro 2/Scripts, and use Scripts > Update Scripts Menu, it operates just fine over here when a group is selected.
property myMonth : {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"}
global myYear
set myYear to year of (current date)
tell application id "com.devon-technologies.thinkpro2"
if the selection is {} then error "Nothing selected. Please select a group."
set thisItem to the first item of (the selection as list)
if the type of thisItem is not group then error "Selection is not a group. Please select a group."
set myYear to text returned of (display dialog "Enter year as YYYY:" default answer myYear with title "Enter year")
set anchorFolder to (name of thisItem) as string
set loopMyMonth to 1
repeat until loopMyMonth = 13
set monthFolder to anchorFolder & "/" & myYear & "-" & (item loopMyMonth in myMonth) & "-YTD"
create location monthFolder
set loopMyMonth to loopMyMonth + 1
end repeat
end tell
If you continue to have trouble executing the script, try locating it in your user’s global script menu at ~/Library/Scripts.
Just curious, I guess it’s a stupid question. I tried to recreate errors (nothing selected or selected item is not a group). Error message pops up in Apple Script editor whereas nothing happens if I run the script from DEVONthink.
Indeed … you’re right. I don’t know why, other than I made a syntax error.
So, here’s an revised approach that actually throws off the error messages … at least it does over here
--Create a set of 12 subgroups of a selected group.
--20121106
--Concept and original of this script courtesty of @andrej
--See https://discourse.devontechnologies.com/t/create-a-set-of-sub-folders/14817/2
property myMonth : {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"}
global myYear
set myYear to year of (current date)
try
tell application id "com.devon-technologies.thinkpro2"
if the selection is {} then
display dialog "Nothing selected. Please select a group"
error "Nothing selected. Please select a group."
end if
set thisItem to the first item of (the selection as list)
if the type of thisItem is not group then
display dialog "Selection is not a group. Please select a group."
error "Selection is not a group. Please select a group."
end if
set myYear to text returned of (display dialog "Enter year as YYYY:" default answer myYear with title "Enter year")
set anchorFolder to (name of thisItem) as string
set loopMyMonth to 1
repeat until loopMyMonth = 13
set monthFolder to anchorFolder & "/" & myYear & "-" & (item loopMyMonth in myMonth) & "-YTD"
create location monthFolder
set loopMyMonth to loopMyMonth + 1
end repeat
end tell
end try
I noticed script works just for groups (folders) in the root level. If I try to run it on any sub-group it doesn’t create the set of groups. It’s not vital as I can move folders but I’m curious why is that?
One more observation. If the selection is sub-group the script re-creates the group with the same name at the root level and correctly generates the set ob sub-folders in YYYY-MM-YTD format.
Your “anchorFolder” variable is used as the database “location” (i.e., the parent folder) for your new groups. By setting it to “name of thisItem” the script will always create a folder with the database root as parent (it it doesn’t exist - or use the existing location if it does) and create new folders under that location if they don’t exist, or leave existing folders with those names if they do. (“create location” will not create a duplicate location, it only creates a new location if there isn’t already a location with that name.)
If you don’t want the behaviors you mention, then make these changes
CHANGE: set anchorFolder to (name of thisItem) as string
TO: set anchorFolder to (location of thisItem) & (name of thisItem) & "/"
CHANGE: set monthFolder to anchorFolder & "/" & myYear & "-" & (item loopMyMonth in myMonth) & "-YTD"
TO: set monthFolder to anchorFolder & myYear & "-" & (item loopMyMonth in myMonth) & "-YTD"
Note that I moved the virgule (“/”) to where “anchorFolder” is defined because IMO that makes the code more readable. Just a fussy change.