I been reading many documents and posts on the internet and I am not able to make enough sense on what I am trying to do.
I have cobbled some applescript code which works as I wanted as long as the parent folder does not exist already. If the parent folder is present I receive an error an the script stops. I’d like for it to process all the subfolders into the existing folder instead of erroring and stopping. As I am not very knowledgable, I am not sure how to do this. Every attempt I have made to correct the error created a new error so now I am seeking help. here is the code that I am using.
-- set date format
on todayISOformat()
set theDate to current date
set y to text -4 thru -1 of ("0000" & (year of theDate))
set m to text -2 thru -1 of ("00" & ((month of theDate) as integer))
set d to text -2 thru -1 of ("00" & (day of theDate))
return m & d & y
end todayISOformat
-- make a folder structure
tell application "Finder" to try -- get the current selection
set ThePath to the first item of (get selection)
if the last character of (ThePath as text) is not ":" then -- a file
set ThePath to the container of ThePath
end if
on error -- default to the desktop
set ThePath to path to desktop
end try
set JobName to text returned of (display dialog "Please enter Work Order:" default answer "Work Order")
set textYear to text -2 through -1 of ((current date)'s year as text)
set JobNum to text returned of (display dialog "Please enter date:" default answer {todayISOformat()})
set JobNumName to JobNum
MakeFolderStructure out of {JobNumName, {JobName, {"scopes", "completed", {"images"}}}} at ThePath
to MakeFolderStructure out of SomeItem at SomeFolder
(*
make the folder structure defined in SomeItem at SomeFolder
SomeList defines the structure:
nested list items create folders in the previous text item - {"A", {"B", {"C"}}} = /A/B/C
empty text items will create untitled folders
parameters - SomeItem [mixed]: the folder structure
SomeFolder [alias]: the destination folder
returns nothing
*)
set ParentFolder to SomeFolder
if class of SomeItem is list then
repeat with AnItem in SomeItem
if class of AnItem is list then -- add subfolder(s)
MakeFolderStructure out of AnItem at SomeFolder
else -- add a new child folder at the current parent
tell application "Finder" to make new folder at ParentFolder with properties {name:AnItem}
set SomeFolder to the result as alias
end if
end repeat
else -- add a new (potential) parent folder
tell application "Finder"
if not (exists folder SomeFolder) then make new folder at SomeFolder with properties {name:AnItem}
set ParentFolder to the result as alias
end tell
end if
end MakeFolderStructure
I use the this as a button in my finder toolbar to create the directories in whatever folder is open or to default to the desktop.
If one of you who are way more knowledgable could show me how to correct this and explain how it works so that I will know in the future, I’d be grateful.
tell application "Finder"
if exists SomeFolder then
set ParentFolder to SomeFolder
else
set ParentFolder to make new folder SomeFolder
end if
end tell
It is a good start for a freshman! There is however a lot you can do to make the process easier, there should also be a lot of scripts here, almost like yours, but more effective. The most usual thing is to exploit the mkdir command, together with braces expansion from a do shell script statement. This makes the whole three in one single command. StefanK and Nigel Garvey have for sure written some of these, in case you want to search by Author for them.
this is a version with mkdir and an easier date creation handler
-- set date format
on todayISOformat()
tell (current date) to return ((its month as integer) * 1000000 + (its day) * 10000 + (its year)) as text
end todayISOformat
-- make a folder structure
tell application "Finder" to try -- get the current selection
set ThePath to the first item of (get selection)
if class of ThePath is not folder then -- a file
set ThePath to the container of ThePath as alias
end if
on error -- default to the desktop
set ThePath to path to desktop
end try
set JobName to text returned of (display dialog "Please enter Work Order:" default answer "Work Order")
set JobNum to text returned of (display dialog "Please enter date:" default answer {todayISOformat()})
set JobNumName to JobNum
tell application "Finder" to exists folder JobName of folder JobNumName of ThePath
if result then
display dialog "The folder " & JobName & " already exists"
else
do shell script "/bin/mkdir -p " & quoted form of POSIX path of ThePath & quoted form of JobNumName & "/" & quoted form of JobName & "/{scopes,completed/images}"
end if
on todayISOformat()
tell (current date) to return text 2 thru -1 of ((100000000 + (its month) * 1000000 + (its day) * 10000 + (its year)) as text)
end todayISOformat
. for the leading zero on the month when required.
It looks fine but if you compare its execution time to the one given by Nigel you will see that Nigel’s one is faster than the one using do sell script.
At least it was faster under Mavericks, I didn’t tested again under 10.10.x.
I am not sure what I did wrong, but I have replaces the script with this
-- set date format
on todayISOformat()
tell (current date) to return text 2 thru -1 of ((100000000 + (its month) * 1000000 + (its day) * 10000 + (its year)) as text)
end todayISOformat
-- make a folder structure
tell application "Finder" to try -- get the current selection
set ThePath to the first item of (get selection)
if class of ThePath is not folder then -- a file
set ThePath to the container of ThePath as alias
end if
on error -- default to the desktop
set ThePath to path to desktop
end try
set JobName to text returned of (display dialog "Please enter Work Order:" default answer "Work Order")
set JobNum to text returned of (display dialog "Please enter date:" default answer {todayISOformat()})
set JobNumName to JobNum
tell application "Finder" to exists folder JobName of folder JobNumName of ThePath
if result then
display dialog "The folder " & JobName & " already exists"
else
do shell script "/bin/mkdir -p " & quoted form of POSIX path of ThePath & quoted form of JobNumName & "/" & quoted form of JobName & "/{scopes,completed/images}"
end if
I get this error
This only takes place if parent folder aka the date folder name, already exists.
You kept in secret that you want to use the code in an Automator workflow
In an Run AppleScript action the code should look like
on todayISOformat()
tell (current date) to return text 2 thru -1 of ((100000000 + (its month) * 1000000 + (its day) * 10000 + (its year)) as text)
end todayISOformat
on run {input, parameters}
-- set date format
-- make a folder structure
tell application "Finder" to try -- get the current selection
set ThePath to the first item of (get selection)
if class of ThePath is not folder then -- a file
set ThePath to the container of ThePath as alias
end if
on error -- default to the desktop
set ThePath to path to desktop
end try
set JobName to text returned of (display dialog "Please enter Work Order:" default answer "Work Order")
set JobNum to text returned of (display dialog "Please enter date:" default answer {todayISOformat()})
set JobNumName to JobNum
tell application "Finder" to exists folder JobName of folder JobNumName of ThePath
if result then
display dialog "The folder " & JobName & " already exists"
else
do shell script "/bin/mkdir -p " & quoted form of POSIX path of ThePath & quoted form of JobNumName & "/" & quoted form of JobName & "/{scopes,completed/images}"
end if
return input
end run
I am sorry I kept in secret out of ignorance, i did not realize there were differences. I pasted the new code provided below in my window i pressed the hammer the colors change to the new colors I save it and test it at receive the same exact error, i am pretty sure I am doing something wrong,
this is how I am using the .app I used option command to add it to the finder toolbar. I choose December then click the app button on the finder toolbar. if since today is 12032014, if the folder is not present all is well and it creates it. I then click the button to try and a new work order with parent 12032014 folder and this is when it yells at me with that error.
If it helps, I found the applescript editor and I pasted the other code into it and tried it and was given this error
do shell script "/bin/mkdir -p " & quoted form of POSIX path of ThePath & quoted form of JobNumName & "/" & quoted form of JobName & "/{scopes,completed/images}"
end if
by
do shell script "/bin/mkdir -p " & quoted form of posix path of( ThePath & JobNumName & ":" & JobName) & "/{scopes,completed/images}"
end if
The following change worked, thank you so much for your patience in helping me.
now to keep learning as there are many other things I am going to be using this for now that I am now a mac user from pc