Applescript download files from Http server sporadic results - applescript

I wrote a script that downloads files from an Http server, but the results are very sporadic. If I run it three times in a row, it might work twice and error once or not work at all and return different errors.
Some of the errors I am getting are:
Error with downloading target URL (-609)
Url Access Scripting got an error: Connection is invalid.
Error with downloading target URL (-31040)
URL Access Scripting got an error: An error of type -31040 has occurred.
try
set theFileURL to "http://ftp2.nflfilmstv.com/filmsint/ftp-inet/Team/110915_game_preview_phi_atl_3200k.mp4" as text
set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set theFile to text item -1 of theFileURL
set AppleScript's text item delimiters to TID
set theFilePath to "Macintosh HD:Users:rgilkes:Desktop:" & theFile as text
tell application "URL Access Scripting" to download theFileURL to file theFilePath with progress
on error ErrorMessage number ErrorNumber
display alert "Error with downloading target URL (" & ErrorNumber & ")" message ErrorMessage
end try
Is there a better way to download files via AppleScript or is my coding bad?

Ahhh, thanks for the preview! I'm originally a Philadelphian and still passionate about Philly sports. I hope I'm not helping a Falcons fan. At least not this week! ;)
Anyway your code looks fine although URLAccessScripting is not the most reliable way to download. Actually as of 10.7 it's no longer even is included with the OS. Curl is an alternative and usually stable. You won't get a progress window with it though. Try this. See if it's more stable. It will tell you when it's finished.
set theFileURL to "http://ftp2.nflfilmstv.com/filmsint/ftp-inet/Team/110915_game_preview_phi_atl_3200k.mp4"
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
set theFile to text item -1 of theFileURL
set AppleScript's text item delimiters to TID
set theFilePath to (path to desktop as text) & theFile
try
do shell script "curl " & quoted form of theFileURL & " -o " & quoted form of POSIX path of theFilePath
display dialog "The download is finished!" buttons {"OK"} default button 1 with icon note giving up after 5
on error theError
display dialog "Error downloading the file:" & return & theFile & return & return & theError buttons {"OK"} default button 1 with icon 0 giving up after 5
end try

Related

Finding a file using AppleScript

someone please explain why this won't work. The script alone without the "if mainReturned = "File" then" works just fine but when I add it to another part of a script, things get weird. It highlights the word "on" on the 5th to last line of the script and then stops working etc. Please explain how I can fix it to add it to another script? The gist of the other script is basically if you enter a word the script will bring certain things up such as if I were to enter the work "File" it performs this. But it won't work. Its the format of if mainReturned = "File" then
nothing really this is kinda the only code I could minimize the script to
set SearchDir to choose folder
set SearchTerm to text returned of ¬
(display dialog "Enter your search term below:" default answer "" with icon file "Macintosh HD:Users:<insert user's name.:Documents:robot-icon.png")
set theResults to SpotlightSearch(SearchTerm, SearchDir)
if theResults is not {""} then
set theChoice to choose from list theResults with prompt ¬
"Here are the files that match the search term \"" & SearchTerm & ¬
"\" in directory \"" & SearchDir & "\":" cancel button name ¬
"Quit" OK button name "Reveal in Finder"
if theChoice is not false then
tell application "Finder"
activate
reveal (POSIX file theChoice) as alias
end tell
end if
else
display dialog "No Results Found." buttons ¬
{"Quit", "Search Again"} default button 2 with icon file "Macintosh HD:Users:<insert user's name>:Documents:robot-icon.png"
if button returned of result is "Quit" then exit repeat
end if
on SpotlightSearch(SearchTerm, SearchDir)
set theResults to paragraphs of ¬
(do shell script "mdfind " & quoted form of SearchTerm & ¬
" -onlyin " & quoted form of POSIX path of SearchDir)
end SpotlightSearch
my expected results is -
if mainReturned = "File" then
display dialog "Keyword '" & mainReturned & "' is being executed..." buttons {"OK"} default button 1 with title "Auto-Web" with icon file "Macintosh HD:Users:<insert user name>:Documents:robot-icon.png" giving up after 1
set SearchDir to choose folder
set SearchTerm to text returned of ¬
(display dialog "Enter your search term below:" default answer "" with icon file "Macintosh HD:Users:<insert user's name.:Documents:robot-icon.png")
set theResults to SpotlightSearch(SearchTerm, SearchDir)
if theResults is not {""} then
set theChoice to choose from list theResults with prompt ¬
"Here are the files that match the search term \"" & SearchTerm & ¬
"\" in directory \"" & SearchDir & "\":" cancel button name ¬
"Quit" OK button name "Reveal in Finder"
if theChoice is not false then
tell application "Finder"
activate
reveal (POSIX file theChoice) as alias
end tell
end if
else
display dialog "No Results Found." buttons ¬
{"Quit", "Search Again"} default button 2 with icon file "Macintosh HD:Users:<insert user's name>:Documents:robot-icon.png"
if button returned of result is "Quit" then exit repeat
end if
on SpotlightSearch(SearchTerm, SearchDir)
set theResults to paragraphs of ¬
(do shell script "mdfind " & quoted form of SearchTerm & ¬
" -onlyin " & quoted form of POSIX path of SearchDir)
end SpotlightSearch
but what really happens is the script highlights the word "on" the 5th to last line of the code and says Syntax Error:Expected “else”, etc. but found “on”. Can someone please make it so this code is compatible with what I am trying to do? It would be most appreciated
There isn't enough context to provide a functioning reply, but you are missing the corresponding end if statement for your added if mainReturned = "File" statement. It helps a bit if you format and/or comment your code to group things together so that you don't get lost.
The error is pretty clear: You have to balance the if statement with an end if before the SpotlightSearch handler
if mainReturned = "File" then
...
if theResults is not {""} then
...
else
...
end if
end if
on SpotlightSearch(SearchTerm, SearchDir)
set theResults to paragraphs of ¬
(do shell script "mdfind " & quoted form of SearchTerm & ¬
" -onlyin " & quoted form of POSIX path of SearchDir)
end SpotlightSearch
Thank y'all so much for your help and being willing to help!!! I have learned so much from y'all and I love this website and the kindness presented on it. I have found the answer. I needed to create a second script and input it into the applications script folder and then use the command run script file " THANK YOU FOR THE HELP!!!!!!

Make multiple folders from excel using AppleScript with specific naming structure

I need to make thousands of folders from columns from excel. I've looked around the internet and have got close, but I need the naming structure to be
"LASTNAME, FIRSTNAME - ID"
Each of those are in a separate column in excel.
I've tried the code below and it works, but the naming structure is not what I want it to be.
display dialog " --Make Lotsa' Folders--
This AppleScript will create multiple folders using names from a text file that you specify."
set destination to (choose folder with prompt "Where would you like to make the folders?")
set textFile to (choose file with prompt "Select the text file you wish to use")
set folderNames to paragraphs of (read textFile)
repeat with oneName in folderNames
tell application "Finder" to make new folder at destination with properties {name:oneName}
end repeat
display dialog "Your folders are done." buttons {"OK"}
https://imgur.com/a/T2ske
https://imgur.com/a/L2al5 This is what the folder filename would be.
Please try this:
set destination to (choose folder with prompt "Where would you like to make the folders?")
tell application "Microsoft Excel" to set usedRange to string value of used range of sheet 1
repeat with i from 2 to (count usedRange)
tell item i of usedRange to set folderName to item 1 & ", " & item 2 & " - " & item 3
tell application "Finder" to make new folder at destination with properties {name:folderName}
end repeat
display dialog "Your folders are done." buttons {"OK"}
As mentioned in the comments the Finder is very very slow while creating the folders one by one, this is a faster solution keeping the way to parse Excel (which is pretty fast by the way) but using the shell mkdir command with a composed parameter list to create the folders.
set destination to (choose folder with prompt "Where would you like to make the folders?")
tell application "Microsoft Excel" to set usedRange to string value of used range of sheet 1
set nameList to {}
repeat with i from 2 to (count usedRange)
tell item i of usedRange to set end of nameList to "'" & item 1 & ", " & item 2 & " - " & item 3 & "'"
end repeat
set {TID, text item delimiters} to {text item delimiters, ","}
set mkdirParams to "/{" & (nameList as text) & "}"
set text item delimiters to TID
do shell script "/bin/mkdir " & quoted form of POSIX path of destination & mkdirParams
display dialog "Your folders are done." buttons {"OK"}

Applescript to attach files and then send email to a recipient works if the script is on DT or in Doc folder, but not if placed in Dropbox folder

I am running Apple OS X 10.11.1 on a Macbook Pro. I use OpenOffice Calc ver 4.1.2 Macros to download SQL data from the internet, do calculations on the data, then create pdf files of the results, which are sent out to folks. The Emailing routines built into OpenOffice Basic work fine on a PC running Windows 7, but on the Mac, The Recipient and Subject fields are not passed to the Mac Mail.app. To solve the problem, I test to see if the file path is PC centric or Mac Centric, and if Mac Centric, I create a text file that has the Mac Centric paths to all the pdf files, then call an Applescript that reads the text file data, adds a subject line and recipient EM address, attaches the pdf files to the email message, then send the email to the recipient, without any intervention from the user. If the script is on the Desktop or in the Documents folder, it works fine, but not if it is placed in my Dropbox folder. I Get the "Network file permission error. (-5000). I tried to fix the File Permissions by going to Dropbox preferences, holding the options key down and clicking the "Fix Permissions" button, but that does not work either.
Can anyone tell me what to do to fix this. I have included the Applescript and text file so you can see what happens.
on run
(* tell application "Finder"
open file "somefile.txt" of folder of (file (path to me))
end tell *)
set myPath to (path to me) as text
set MME_path to POSIX path of myPath
set b to MME_path
set text item delimiters to "/"
set d to {}
set c to text items of b
set myhome to item 3 of c
-- display dialog MME_path & " " & myhome
set myPrefsFile to item 2 of c & "/" & item 3 of c & "/" & item 4 of c & "/" & item 5 of c & "/EM-Eargv.txt"
open for access myPrefsFile
set PrefsContents to read myPrefsFile using delimiter {","}
close access myPrefsFile
set thesubject to item 1 of PrefsContents
set thebody to ""
set theSender to item 2 of PrefsContents
set theAddress to item 3 of PrefsContents
set theAttachment to item 4 of PrefsContents
set theAttachment1 to POSIX file theAttachment
set theAttachment to item 5 of PrefsContents
set theAttachment2 to POSIX file theAttachment
set theAttachment to item 6 of PrefsContents
set theAttachment3 to POSIX file theAttachment
set theAttachment to item 7 of PrefsContents
set theAttachment4 to POSIX file theAttachment
--set userCanceled to false
--try
-- set dialogResult to display dialog ¬
-- "Path to file is " & theAttachment & " POSIX " & theAttachment1 buttons {"Cancel", "OK"} ¬
-- default button "OK" cancel button ¬
-- "Cancel" giving up after 15 ¬
-- default answer (long user name of (system info))
--on error number -128
-- set userCanceled to true
--end try
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:thesubject, content:thebody}
tell newMessage
set visible to true
set sender to theSender
make new to recipient at end of to recipients with properties {name:theSender, address:theAddress}
tell content
make new attachment with properties {file name:theAttachment1} at after the last paragraph
make new attachment with properties {file name:theAttachment2} at after the last paragraph
make new attachment with properties {file name:theAttachment3} at after the last paragraph
make new attachment with properties {file name:theAttachment4} at after the last paragraph
end tell
end tell
delay 3
send newMessage
end tell
delay 3
(*
tell application "Mail"
quit
end tell
*)
end run
The Text File is called EM-Eargv.txt
EM-Docs Files FYI.,”Your Name”,”YourEMaddress”,file:///Users/admin/Documents/Dropbox/EM-Docs/Sends/Doc1.pdf,file:///Users/admin/Documents/Dropbox/EM-Docs/Sends/Doc2.pdf,file:///Users/admin/Documents/Dropbox/EM-Docs/Sends/Doc3.pdf,file:///Users/admin/Documents/Dropbox/EM-Docs/Sends/Doc4.pdf,
You will have to update this text file with your own Name and email address plus change the path to whatever your is and place 4 short PDF files, named Doc1.pdf, Doc2.pdf, Doc3.pdf and Doc4.pdf in a folder called "Sends", before this applescript will send the email to you.
I can send you a short OpenOffice Calc document that creates the Text File with the correct path file data for your computer's configuration but I do not see how to attach the Calc.ods file into this request.
I run the entire example I have from a directory called "EM-Docs" That I can create a Zip file to be downloaded, but again, I do not see a way to attach a zip file to this request.
I'm guessing, it's this code block that's throwing the error:
open for access myPrefsFile
set PrefsContents to read myPrefsFile using delimiter {","}
close access myPrefsFile
Place some display dialog messages to debug and find the exact code, like so:
display dialog "About to read file"
open for access myPrefsFile
set PrefsContents to read myPrefsFile using delimiter {","}
close access myPrefsFile
display dialog "Just finished reading file" & return & (PresContents as string)
But here's the thing, if the problem is the open for access conflict with Dropbox, you don't HAVE to open for access a file if all you're doing is reading it. Just removed the open/close access lines, and try that:
display dialog "About to read file"
set PrefsContents to read myPrefsFile using delimiter {","}
display dialog "Just finished reading file" & return & (PresContents as string)
If, for some reason you do need to open file access, you could always quit Dropbox, do the file work, then launch Dropbox.

Getting the file extension of an image

I'm modifying an AppleScript I found online for uploading images to Imgur. I'm having difficulty extracting the file extension though--despite what I've found online, nothing seems to work.
I'm trying to set a variable to the file extension (i.e. ".png", ".jpg", etc.) with the quotes around them.
on run {input, parameters}
tell application "Finder"
-- convert file paths to posix
set imagePath to POSIX path of (input as text)
-- debug display dialog imagePath as text with title "Path of File" buttons {"Okay"} default button "Okay"
--debug display dialog input as text with title "Input"
set ext to name extension of input
set apiKey to "26ff5c40cbedf50e7f81124ab473c1cc"
set curlCommand to "curl -F \"key=" & apiKey & "\" -F \"image=#" & imagePath & "\" http://api.imgur.com/2/upload"
set answer to do shell script curlCommand
set atid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "<imgur_page>"
set link to text item 2 of answer
set AppleScript's text item delimiters to "<"
set link to text item 1 of link & (ext as text) --will be a direct link to the file, but the extension of the original file will matter
set AppleScript's text item delimiters to atid
set the clipboard to link
display notification "URL copied to clipboard" subtitle "Upload to Imgur complete"
end tell
end run
Instead of:
set ext to name extension of input
Try using:
set ext to (last text item of input)
This should define ext as the last text item after . in input

Creating nested folders with variable names using Applescript/mkdir

I'm trying to create an applescript that prompts for a job name, then a desired directory in which to create a folder with the job name. It then should create a series of subfolders within the initial folder with names such as "job name-Links" & "job name-PDFs" etc.
I've cobbled this script together from different sources & would like feedback on how to improve it, because I'm sure it's ugly as sin.
tell application "Finder"
activate
set jobNum to text returned of (display dialog "Enter a job number:" default answer "[JobNumber]-[Description]")
set folderpath to POSIX path of (choose folder with prompt "Select client folder")
do shell script "/bin/mkdir -p " & quoted form of folderpath & "/" & quoted form of (jobNum) & "/" & quoted form of (jobNum & "-Links")
do shell script "/bin/mkdir -p " & quoted form of folderpath & "/" & quoted form of (jobNum) & "/" & quoted form of (jobNum & "-PDFs")
do shell script "/bin/mkdir -p " & quoted form of folderpath & "/" & quoted form of (jobNum) & "/" & quoted form of (jobNum & "-Supplied")
do shell script "/bin/mkdir -p " & quoted form of folderpath & "/" & quoted form of (jobNum) & "/" & quoted form of (jobNum & "-Versions")
end tell
Any thoughts & comments would be greatly appreciated. This is my first attempt at a script. I thought I'd post this because I've hunted for this specific use case for ages and only found bits and pieces. Hopefully someone can help me out so other will find it useful too.
Another idea would be to create a template of the desired folder structure somewhere, then have the script copy that structure with the client prefix. A couple of benefits to this method are that you don't need to change the script if folders are added to the template (the handler will also add to existing structures), and you don't have to figure out how to script complex (or large) structures.
property template : "/path/to/template/folder" -- the folder structure to copy from
on run -- example
set jobNum to text returned of (display dialog "Enter a job number:" default answer "[JobNumber]-[Description]")
set folderPath to (choose folder with prompt "Select a location for the client folder")
tell application "Finder" to try
set clientFolder to (make new folder at folderPath with properties {name:jobNum})
on error number -48 -- folder already exists
set clientFolder to (folderPath as text) & jobNum
end try
copyFolderStructure_toFolder_withPrefix_(template, clientFolder, jobNum)
end run
to copyFolderStructure_toFolder_withPrefix_(source, destination, additions) -- copy folder structure using mkdir
set {source, destination} to {source as text, destination as text}
if source begins with "/" then set source to POSIX file source
if destination begins with "/" then set destination to POSIX file destination
set {source, destination} to {source as alias, destination as alias}
set additions to additions as text
tell application "Finder" to set theFolders to (folders of entire contents of source) as alias list
set folderNames to ""
repeat with someFolder in theFolders -- set up the folder names parameter for the shell script
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set namePieces to text items of (text ((count (source as text)) + 1) thru -2 of (someFolder as text))
set AppleScript's text item delimiters to ("/" & additions)
set namePieces to space & quoted form of (additions & (namePieces as text))
set AppleScript's text item delimiters to tempTID
set folderNames to folderNames & namePieces
end repeat
do shell script "cd " & quoted form of POSIX path of destination & "; mkdir -p" & folderNames
end copyFolderStructure_toFolder_withPrefix_
You can do something like this:
set TID to AppleScript's text item delimiters
-- Ensure the answer is formatted properly
repeat
try
set theAnswer to text returned of (display dialog "Enter a job number:" default answer "[JobNumber]-[Description]")
set AppleScript's text item delimiters to "-"
set {jobNum, jobDes} to {text item 1 of theAnswer, text item 2 of theAnswer}
exit repeat
on error
display dialog "Please enter your response in this format [JobNumber]-[Description]" buttons {"Cancel", "Try again"} default button "Try again"
end try
end repeat
set folderpath to POSIX path of (choose folder with prompt "Select client folder")
set folderNames to {"-Links", "-PDFs", "-Supplied", "-Versions"}
repeat with aName in folderNames
do shell script "mkdir -p " & quoted form of (folderpath & jobNum & "-" & jobDes & "/" & jobNum & "-" & jobDes & aName as text)
end repeat
set AppleScript's text item delimiters to TID
mkdir can create a hierarchy, just put these names in the {}
Example : '/destFolderPath/jobName/prefix'{"suffix1","suffix2","suffix3","suffix4"}
Here is the script.
set jName to my getJobNum()
set folderpath to POSIX path of (choose folder with prompt "Select client folder")
do shell script "mkdir -p " & (quoted form of (folderpath & jName & "/" & jName)) & "{\"-Links\",\"-PDFs\",\"-Supplied\",\"-Versions\"}"
on getJobNum()
activate
text returned of (display dialog "Enter a job number:" default answer "[JobNumber]-[Description]")
tell the result to if it is not "" then return it
getJobNum() -- else text returned is empty
end getJobNum
Important, if you plan to have slashes in the name, you must replace them by the colon character, it's easy to add a handler in the script to replace them, otherwise it will create other subfolders.
I'm no scripter, but I was looking for the same kind of structured folder solution. After going over these other examples, I came up with a slightly modified version for setting up project folders for our group of designers.
set TID to AppleScript's text item delimiters
-- Ensure the answer is formatted properly
repeat
try
set theAnswer to text returned of (display dialog "Please create a job folder, with the name in this format:" default answer "JobNumber-JobCode-ClientDescription")
set AppleScript's text item delimiters to "-"
set {jobNum, JobCod, CliDes} to {text item 1 of theAnswer, text item 2 of theAnswer, text item 3 of theAnswer}
exit repeat
on error
display dialog "Please enter your job details in this format [JobNumber]-[JobCode]-[ClientDescription]" buttons {"Cancel", "Try again"} default button "Try again"
end try
end repeat
set folderpath to POSIX path of (choose folder with prompt "Where to create your project folder?")
set folderNames to {"Links", "Client_input", "SourceBuilds", "Review-Proofs"}
repeat with aName in folderNames
do shell script "mkdir -p " & quoted form of (folderpath & jobNum & "-" & JobCod & "-" & CliDes & "/" & aName as text)
end repeat
set AppleScript's text item delimiters to TID
I'm using a variant of jackjr300 script to generate a website folder template/
do shell script "mkdir -p " & (quoted form of (folderpath & jName & "/")) & "{\"codey\",\"js\",\"fonts\",\"images\"}"
Plus another run to generate the subfolders and populate those with an existing frameworks and scripts with a duplicate folder action.
Very handy Thanks ...!
The first thing you could do to improve your script is don’t use shell scripting. Shell scripting is like a jackhammer, but what you are doing only requires the tiniest hand-held hammer.
Here is your original script:
tell application "Finder"
activate
set jobNum to text returned of (display dialog "Enter a job number:" default answer "[JobNumber]-[Description]")
set folderpath to POSIX path of (choose folder with prompt "Select client folder")
do shell script "/bin/mkdir -p " & quoted form of folderpath & "/" & quoted form of (jobNum) & "/" & quoted form of (jobNum & "-Links")
do shell script "/bin/mkdir -p " & quoted form of folderpath & "/" & quoted form of (jobNum) & "/" & quoted form of (jobNum & "-PDFs")
do shell script "/bin/mkdir -p " & quoted form of folderpath & "/" & quoted form of (jobNum) & "/" & quoted form of (jobNum & "-Supplied")
do shell script "/bin/mkdir -p " & quoted form of folderpath & "/" & quoted form of (jobNum) & "/" & quoted form of (jobNum & "-Versions")
end tell
Compare to a “de-shelled” version, which is easier to read, write, and maintain:
tell application "Finder"
activate
set jobNum to text returned of (display dialog "Enter a job number:" default answer "[JobNumber]-[Description]")
set theClientFolder to (choose folder with prompt "Select client folder")
set theSubFolderNames to {"Links", "PDFs", "Supplied", "Versions"}
repeat with theSubFolderName in theSubFolderNames
make new folder at theClientFolder with properties {name:jobNum & "-" & theSubFolderName}
end repeat
end tell
Notice that the variable “folderpath” has changed to “theClientFolder” because choose folder does not return a path, it returns an alias object for the chosen folder. The command for telling Finder to make a new folder is “make new folder at [alias where you want the folder to be made]” so you don’t need to convert that alias into a path to make new folders inside of it.
Notice also that the names of the folders have been declared as variables in a list, which makes them easier to read or change later. Even somebody who does not know AppleScript could open this script and change the name of the “PDFs” folder to “Documents” or something like that.
Another improvement I could suggest would be to make the way that you’re gathering the input from the user a little more sophisticated. You seem to be asking for 2 items of information other than the client folder — job number and job description — so use 2 dialog boxes, because that will be easier for the user and less error-prone because you can give 2 actual examples of what you want them to input. You can also improve the appearance of those dialogs in a few ways. And you can add a little error handling — if the user cancels the dialogs you’re presenting, that generates errors. You can handle them gracefully by checking if the user clicked “OK” or not.
Here is a version of your script with the user input cleaned up a bit:
tell application "Finder"
activate
display dialog "Enter a job number:" default answer "100" buttons {"Cancel", "OK"} default button "OK" with title (the name as text) with icon note giving up after 180
if the button returned of the result is equal to "OK" then
set theJobNumber to the text returned of the result
display dialog "Enter a job description:" default answer "Photos" buttons {"Cancel", "OK"} default button "OK" with title (the name as text) with icon note giving up after 180
if the button returned of the result is equal to "OK" then
set theJobDescription to the text returned of the result
try
choose folder with prompt "Select client folder:" default location (the path to the desktop folder as alias) without multiple selections allowed, invisibles and showing package contents
set theClientFolder to the result
on error
set theClientFolder to ""
end try
if theClientFolder is not equal to "" then
set theSubFolderNames to {"Links", "PDFs", "Supplied", "Versions"}
repeat with theSubFolderName in theSubFolderNames
make new folder at theClientFolder with properties {name:theJobNumber & "-" & theJobDescription & "-" & theSubFolderName}
end repeat
end if
end if
end if
end tell
This will give you dialog boxes that say “Finder” at the top and include the Finder icon, which will give up after 180 seconds (to prevent at timeout error) and which, if the user presses “Cancel” at any point, will just die gracefully without generating any errors. You can tweak the default answers for the input dialogs so that they help the user put in the right input, for example, if job numbers are 4 digits, put a 4 digit number here. And the choose folder prompt will start by showing the Desktop — you can change that to another location if there is a disk or folder where you store all your client folders:
choose folder with prompt "Select client folder:" default location (disk "Clients" as alias) …
To be clear, shell scripting is awesome, but I recommend you only use “do shell script” to get at unique features of the UNIX layer, like PHP functions, Perl regex, and so on. Anything that is already available to you in the Mac layer is going to be easier and safer to use, and easier to read, write, and maintain in AppleScript. Especially really basic things like creating folders.
I have a solution that copies the folder from a template location. It also creates a shortcut that allows to email the design manager with the subject as the job number/project number
global jobNum
global newJobFolder
set jobNum to text returned of (display dialog "Enter a job number:" default answer "")
set jobName to text returned of (display dialog "Enter a job name:" default answer "")
set jobMgr to text returned of (display dialog "Enter account manager email:" default answer "")
set folderpath to (choose folder with prompt "Select client folder")
set newJobFolder to my newFold(jobNum, jobName, folderpath, jobMgr)
on newFold(theNumber, theName, thefolder, jobMgr)
set emailAddress to jobMgr
set emailSubject to theNumber & " -" & theName
set bodyText to ""
set emailLinkFileName to theNumber & " -" & theName
set subNameList to {"Designs", "Documents", "Received"}
set itemCount to count of subNameList
set linkBody to "mailto:" & emailAddress & "?subject=" & my
replace_chars(emailSubject, " ", "%20") & "&body=" & my
replace_chars(bodyText, " ", "%20")
tell application "Finder"
set newJobFolder to (make new folder at thefolder with properties ¬
{name:theNumber & " " & theName})
repeat with i from 1 to itemCount
set aSubFolder to make new folder at newJobFolder with properties {name:jobNum & " " & item i of subNameList}
set theMailto to make new internet location file to linkBody at aSubFolder with properties {name:emailLinkFileName, name extension:"mailloc"}
set the name extension of theMailto to "mailloc"
end repeat
duplicate file "Users:ace:Dropbox:Company:0000-1_E.xlsx" of startup disk to folder (jobNum & " Documents") of newJobFolder
set name of the result to jobNum & " E.xlsx"
set theMailto to make new internet location file to linkBody at newJobFolder with properties {name:emailLinkFileName, name extension:"mailloc"}
set the name extension of theMailto to "mailloc"
end tell
end newFold
-- Generic find and replace functionality
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars

Resources