I want to create a simple AppleScript:
define a folder (that will get all the images, docs etc.)
read a file (.txt for example)
download all the URLs into the folder with their original name
Currently, my file has one URL by line, no separator.
I found several scripts but they always do something I don't want, don't need or doesn't work.
I found one that is very close to what I want, but it's editing the names in the loop.
I didn't succeed to split the URL and keep the last part.
Here it is:
property desktopPath : path to desktop as string
set n to 1
repeat with urlLine in paragraphs of (read alias (desktopPath & "filename.txt"))
set qURL to quoted form of urlLine
if qURL ≠ "''" then
set dest to quoted form of ¬
(POSIX path of desktopPath & "URLs/" & n & ".jpg")
do shell script "curl " & quoted form of urlLine & " > " & dest
set n to n + 1
end if
end repeat
If you have any other solution, I take.
Thanks in advance
I found a script that is working fine.
You just need to be sure about the txt file: the list inside should not have any text format.
Weirdly I have to do a copy/paste through a text editor to remove every text format.
1/ Put the txt file on your desktop, 2/ Create a "download" folder on your desktop, 3/ Start the script
set theList to (path to desktop as text) & "Download.txt" -- File name with the URLs
set theFolder to (path to desktop as text) & "download" -- Folder name between quotes
set theFolder to quoted form of POSIX path of theFolder
set theImages to read alias theList as list using delimiter linefeed
repeat with i from 1 to count of theImages
set thisItem to item i of theImages
do shell script "cd " & theFolder & "; " & "curl -O " & quoted form of thisItem
end repeat
I have some files in a work folder that contains jobnames_jobnumber /work/jobnameA_001/outbox/filename.mp4, /work/jobnameB_002/outbox/flamename.mp4 e.t.c
I would like to have an applescript get the full POSIX path and extract just the jobnameA_001 part of the path then create a new folder with jobnames_jobnumber_date
Please note the jobnames are client names so they are not fixed length.
Please also note I am a complete newbie and am not sure what to research to find the answers. But i'll try.
Thank you kindly,
Here's code that does what you want:
# Extract all <jobName>_<jobNumber> tokens from matching paths.
set jobNamesWithNumbers to paragraphs of ¬
(do shell script "for p in /work/*/outbox/*.mp4; do echo \"$p\"; done |
awk -F/ '{ print $2 }'")
# Get the date in format YYYY-MM-DD, e.g., "2014-04-13".
set dateString to do shell script "date +'%Y-%m-%d'"
# Specify the target folder in which to create the new folders:
set targetFolder to POSIX path of (path to desktop)
# Build the absolute paths of the folders to create.
set quotedFolderPathList to ""
repeat with itm in jobNamesWithNumbers
set folderPath to (targetFolder & "/" & itm & "_" & dateString)
set quotedFolderPathList to quotedFolderPathList & " " & ¬
quoted form of folderPath
end repeat
# Finally, create the new folders.
do shell script "mkdir -p" & quotedFolderPathList
Note that this relies heavily on using shell (bash) commands invoked with do shell script, which makes for much shorter code.
This comes at the cost of having to understand two languages, however.
Given AppleScript's limitations, it's worth making this learning investment in the long run.
I have a list with folder paths who should concatenate correctly to have multiple sourcefolders backed up into a single source folder with the rsync command.
This is what I have so far:
set sourcefolderlist to {"/Volumes/sourcefolder1", "/Volumes/sourcefolder2"}
set localfolder to quoted form of POSIX path of ("/Users/dfdfdf/destinationfolder")
set allSourceFolders to ""
repeat with oneSourceFolder in sourcefolderlist
set allSourceFolders to ((quoted form of POSIX path of allSourceFolders) & oneSourceFolder)
end repeat
so the source-paths should be in a list which concatenate with POSIX file paths (like localfolder).
The end should look this:
do shell script "rsync -arvuE " & allSourceFolders & " " & localfolder
How can I concatenate the items in sourcefolderlist so that rsync can read the correct POSIX folder paths?
From the man page:
rsync [OPTION]... SRC [SRC]... DEST
I think you just separate them with a " " character.
If you still need a list as source of all sources (comes in handy), loop thru it like this:
set sourcefolderlist to {"/Volumes/sourcefolder1", "/Volumes/sourcefolder2"}
set localfolder to quoted form of POSIX path of ("/Users/dfdfdf/destinationfolder")
set sources to ""
repeat with i from 1 to number of items in sourcefolderlist
set sources to sources & " " & quoted form of (item i of sourcefolderlist as text)
end repeat
log sources -- > (* '/Volumes/sourcefolder1' '/Volumes/sourcefolder2'*)
do shell script "rsync -arvuE " & sources & " " & localfolder
BTW: use AppleScript's built-in code snippets: make a new line, paste "sourcefolderlist" (=the variable containing the list) into the new line and CTRL-Click it. Select "Repeat Routines" and from there "Process every item".
I wrote a Applescript Script library ( Join list items v2 ) to help with joining Applescript list items in which you can also add quotes to the items as they are placed in the final string.
Example of use in this case would be:
#Needed Use Clauses
use script "you Library name here"
--use scripting additions
set sourcefolderlist to {"/Volumes/sourcefolder1", "/Volumes/sourcefolder2"}
set sources to join list items sourcefolderlist using text space with items in single quotes
-- Result--> "'/Volumes/sourcefolder1' '/Volumes/sourcefolder2'"
Also a small explanation about the library (v1) in the post for version 1. Which also has links to Apples sessions on Applescript Script Libraries
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Hope someone can help me,
To start out, my AppleScript skills are almost non-existing.
I get a number of files for archiving purposes in the following format which need sorting. The format is first the device name, followed by a run number and a sample number followed by some text.
Example file: "UZLA 55879 [05x13] september cal run.cdf" (file format varies)
Which needs to be moved into a folder: ~/UZLA 55879 (LCMS)/Run 5/
The device name is fairly random, sometimes just a number sometimes the entire official naming.
The main folder has a secondary item in brackets after the device name which is not in the file name that is being moved. the string before "[" and "(" do match, after the main name it's different.
The subfolder doesn't always exist, when a new run is started the folder /Run 6/ for example might not exist. Their's no 0 padding to the numbers
The files all arrive in the same folder and their should be no other files located in that folder.
To round this of we like to make an alias in a single folder on the main drive (files are moved to external system) for direct access, which is easy for quick last think look up but entirely unwieldy for the whole system (older aliases are deleted from it by other script).
Thanks.
Okay, so this was annoyingly closed, appearance because it only helps me and not random person from the future. Getting help for me was kinda the point. This is where we're at, with thanks to adayzdone:
set myFolder to (choose folder)
tell application "System Events" to set folderName to myFolder's name
set {TID, text item delimiters} to {text item delimiters, {"(", ")"}}
set myText to text item 2 of folderName
set text item delimiters to TID
set myFiles to every paragraph of (do shell script "find " & quoted form of (POSIX path of myFolder) & " \\! -name \".*\" -type f")
repeat with aFile in myFiles
set aFile to aFile as text
tell application "Finder" to set fileName to name of (POSIX file aFile as alias)
set newPath to "~/" & quoted form of (do shell script "echo " & quoted form of fileName & " | sed -e 's/\\[0/\\[/' -e 's/\\([^\\[]*\\)\\[\\([0-9]*\\)x[0-9]*\\].*/\\1(" & myText & ")\\/Run \\2\\//'") as text
do shell script "mkdir -p " & newPath & " ; mv " & quoted form of aFile & space & newPath
end repeat
This gives the following error: error "Can’t get text item 2 of \"testFolder\"." number -1728 from text item 2 of "testFolder"
Let me clarify: I have a bunch of files in a folder named /testFolder/ (always named that, all files enter here) what I want is to move the files into folders and subfolders in a given format based on the file names.
Example: File: /UZLA 55879 [01x05] XXX.cdf
Base name "UZLA 55879", the folder /UZLA 55879 (LCMS)/ exists at the destination. the (LCMS) is irrelevant to the move, it's just extra junk on the folder name, the script should detect that the folder exists (despite what junk comes between the ()" and use it as it's destination. If no folder with that base name exist it can just pop up an error or crash the whole script, that's not really the issue as new base names are rarely created and are named manually (and rather randomly) anyway.
The second part of the name is [01x05] the first part of that, "01" is detected (stripped from its padding zero) and moved into subfolder /Run 1/ (If it's "[05x07] is goes into /Run 7/ etc. the rest of the file name/extention is irrelevant to the move.
Current issue: The script now tries to pull the info from the starting folder to choose the destination folder, the starting folder is (not) named /UZLA 55879 (LCMS)/ it uses that "LCMS" to create the destination folder. (which it can't since the starting folder is 1) not named that and 2)) every destination folder has a different item (some are the same though) between those parentheses so naming the starting folder like that is useless. The script uses " & myText & " for that, that string has to be a random string which is defined by the destination folder not the starting folder.
I would normally not answer a question like this unless the user has put some effort into it, but I am a sucker for regular expressions.
set sourceFolder to "/Users/You/Desktop/testFolder"
set destFolder to "/Users/You/Desktop/testFolder2"
set myFiles to every paragraph of (do shell script "find " & quoted form of sourceFolder & " \\! -name \".*\" -type f")
repeat with aFile in myFiles
set aFile to aFile as text
tell application "Finder" to set fileName to name of (POSIX file aFile as alias)
-- Find name of parent folder
set parentName to do shell script "echo " & quoted form of fileName & " | sed 's/ \\[.*//'"
tell application "System Events" to set parentFolder to POSIX path of (first folder of folder destFolder whose name starts with parentName)
-- Extract the text between ( and )
set myText to do shell script "echo " & quoted form of parentFolder & " | sed 's/.*(\\(.*\\)).*/\\1/'"
set newPath to quoted form of (destFolder & "/" & (do shell script "echo " & quoted form of fileName & " | sed -e 's/\\[0/\\[/' -e 's/\\([^\\[]*\\)\\[\\([0-9]*\\)x[0-9]*\\].*/\\1(" & myText & ")\\/Run \\2\\//'") as text)
do shell script "mkdir -p " & newPath & " ; mv " & quoted form of aFile & space & newPath
end repeat
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