Create Folders Applescript - applescript

I have an Applescript that I use to create job folders. The Applescript asks for the project name and the location where the folder will be stored. After inputting the job name and folder location, the script creates the main folder and four subfolders.
Now I would like the script to create numbered subfolders within one of the current subfolders, with a prompt that asks the user how many folders to create. Is that possible?
tell application "Finder"
set JobName to text returned of (display dialog "Please enter Job Name:" default answer "Job_Name")
set loc to choose folder "Choose Parent Folder Location"
set newfoldername to JobName
set newfo to make new folder at loc with properties {name:newfoldername}
make new folder at newfo with properties {name:"Job Materials"}
make new folder at newfo with properties {name:"Previews"}
make new folder at newfo with properties {name:"PSDs"}
make new folder at newfo with properties {name:"VX"}
end tell

set JobName to text returned of (display dialog "Please enter Job Name:" default answer "Job_Name")
set loc to choose folder "Choose Parent Folder Location"
tell application "Finder"
set newfo to make new folder at loc with properties {name:JobName}
make new folder at newfo with properties {name:"Job Materials"}
make new folder at newfo with properties {name:"Previews"}
set targetFolder to make new folder at newfo with properties {name:"PSDs"}
make new folder at newfo with properties {name:"VX"}
end tell
repeat
set subCount to text returned of (display dialog "How many subfolders?" default answer 3)
try
if subCount ≠ "" then
subCount as integer
exit repeat
end if
end try
end repeat
repeat with i from 1 to subCount
tell application "Finder" to make new folder at targetFolder with properties {name:"Subfolder " & i}
end repeat

Related

Applescript - duplicate file to folder

(noob / novice) here I have done many searches online but cannot get this to work
I am trying to get a file duplicated from a pre-determined location into a newly created folder from within the script.
The script basically asks for a few bits of info and creates a folder structure using the info and a predefined set of folder within. from there I would want it to copy some template files (.psd .fcpbundle etc) from a template folder on the HDD.
my code is as follows now it all works up until it tries to copy the file
tell application "Finder"
set JobName to text returned of (display dialog "Please enter the wedding date" default answer "YYYY/MM/DD") --Asks for date of wedding
set DT to text returned of (display dialog "Please enter the couples name" default answer "Bride & Groom + Surname") --Asks for couples name
set Wedding to text returned of (display dialog "Please enter the type of day" default answer "Wedding") --Asks for type of day, Wedding, Party, etc
--Creates directory with info from above
set loc to choose folder "Please choose where you would like to save the files" --Loc = Location of where directory will be placed
set newfoldername to JobName & " - " & DT & " - " & Wedding --Adds Date, Couples name and event type to make directory name
--Creates parent directory
set newfo to make new folder at loc with properties {name:newfoldername} --Directory name defined
set audio to make new folder at newfo with properties {name:"Audio"} --creates Audio Directory
set doc to make new folder at newfo with properties {name:"Documents"} --creates Documents Directory
set exports to make new folder at newfo with properties {name:"Exports"} --creates Exports Directory
set fpcx to make new folder at newfo with properties {name:"FCPX Library"} --creates FCPX Directory
set filmposter to make new folder at newfo with properties {name:"Film Poster"} --creates FilmPoster Directory
set finaldel to make new folder at newfo with properties {name:"Final Delivery"} --creates Final Delivery Directory
set images to make new folder at newfo with properties {name:"Images"} --creates Images Directory
set rawcards to make new folder at newfo with properties {name:"RAW Cards"} --creates RAW Cards Directory
set xml to make new folder at newfo with properties {name:"XML"} --creates XML Directory
--Creates sub-folders
set submusic to make new folder at audio with properties {name:"Music"} --creates Music in Audio Directory
set subrawaudio to make new folder at audio with properties {name:"RAW Audio Cards"} --creates RAW Audio Cards in Audio Directory
set subdvd to make new folder at finaldel with properties {name:"DVD"} --creates DVD in Final Delivery Directory
set subusb to make new folder at finaldel with properties {name:"USB"} --creates USB in Final Delivery Directory
set subweb to make new folder at finaldel with properties {name:"WEB"} --creates WEB in Final Delivery Directory
set subgraphics to make new folder at images with properties {name:"Graphics"} --creates Graphics in Images Directory
set substills to make new folder at images with properties {name:"Stills"} --creates Stills in Graphics Directory
set subcam_1 to make new folder at rawcards with properties {name:"Cam-1"} --creates Cam-1 in RAW Cards Directory
set subcam_2 to make new folder at rawcards with properties {name:"Cam-2"} --creates Cam-2 in RAW Cards Directory
set subcam_3 to make new folder at rawcards with properties {name:"Cam-3"} --creates Cam-3 in RAW Cards Directory
--Moves files from template directory to working directory
set sourceFile to POSIX file "/Users/ricoh-admin/Movies/WeddingTemplate Creator/2005_0001.rtf"
set destFolder to POSIX file "loc & finaldel & subdvd"
tell application "Finder"
duplicate POSIX file sourceFile to destFolder
end tell
end tell
Any help would be appreciated and to know where I went wrong etc
You are mixing up POSIX paths and HFS paths.
This version creates the entire folder structure in one line via the shell (this requires a slash separated POSIX path)
The Finder duplicates the RTF file which requires colon separated HFS paths.
tell application "Finder" to set frontmost to true
set JobName to text returned of (display dialog "Please enter the wedding date" default answer "YYYY/MM/DD") --Asks for date of wedding
set DT to text returned of (display dialog "Please enter the couples name" default answer "Bride & Groom + Surname") --Asks for couples name
set Wedding to text returned of (display dialog "Please enter the type of day" default answer "Wedding") --Asks for type of day, Wedding, Party, etc
--Creates directory with info from above
set loc to (choose folder "Please choose where you would like to save the files") as text --Loc = Location of where directory will be placed
set newfoldername to JobName & " - " & DT & " - " & Wedding --Adds Date, Couples name and event type to make directory name
set folderStructure to "/{Audio/{Music,RAW\\ Audio\\ Cards},Documents,Exports,FCPX\\ Library,Film\\ Poster,Final\\ Delivery/{DVD,USB,WEB},Images/{Graphics,Stills},RAW\\ Cards/{Cam-1,Cam-2,Cam-3},XML}"
do shell script "mkdir -p " & quoted form of POSIX path of (loc & newfoldername) & folderStructure
set sourceFile to (path to movies folder as text) & "WeddingTemplate Creator:2005_0001.rtf"
set destFolder to loc & newfoldername & ":Final Delivery:DVD:"
tell application "Finder"
duplicate file sourceFile to folder destFolder
end tell

Applescript - New file SHORTEST WAY

so where this question was all about was just that I forgot to change new file to new folder. (Because I just copy, pasted the code a few times.)
Regarding to the answer of vadian, here is the new code (edited):
property rustItems : {"Render.plist", "KleurInstellingen.txt", "Vrienden.txt", "Vijanden.txt", "Rust-Python-Embed.py", ¬
"Std__PATH.plist", "CargoRun.sett", "Rust-Package.fix", "Rust-Package-Auto.app", ¬
"Rust-PKG.pkg", "Project-Layout.jar", "Run-EXP.gib", "Radar.jar"}
property displayItems : {"Commons-io-2.4.jar", "commons-io-2.4.jar.sha", "Lastlog.log.gz", "Lastlog2.log.gz", ¬
"windows.pkg", "ModuleSettings.pkg", "ModuleStates.pkg", "ColorBinds.pkg", "ModsReturn.pkg", "Textures_1.png", "Textures_2.png", "Textures_3.png"}
property jortpepeItems : {"Rust.class", "Rust$1.class"}
property utilityItems : {"Layout.class", "XYZ__Layout.class"}
property managerItems : {"Restrictions.class", "MAX__DELAY.class"}
property listenerItems : {"OtherEvents.class", "PlayerEvents.class", "PlayerEvents$1.class", ¬
"PlayerChat.class", "PlayerChat$1.class"}
set chosenfolder to choose folder with prompt "Choose the Game folder to install the mods."
tell application "Finder"
if exists folder "Rust$1" of chosenfolder then delete folder "Rust$1" of chosenfolder
set modsFolder to make new folder at chosenfolder with properties {name:"Rust$1"}
repeat with anItem in rustItems
make new file at modsFolder with properties {name:anItem}
end repeat
set displayFolder to make new folder at modsFolder with properties {name:"Display"}
repeat with anItem in displayItems
make new file at displayFolder with properties {name:anItem}
end repeat
set jortpepeFolder to make new folder at modsFolder with properties {name:"jortpepe"}
repeat with anItem in jortpepeItems
make new file at jortpepeFolder with properties {name:anItem}
end repeat
set utilityFolder to make new folder at jortpepeFolder with properties {name:"Utilities"}
set managerFolder to make new folder at jortpepeFolder with properties {name:"Managers"}
set listenerFolder to make new folder at jortpepeFolder with properties {name:"Listeners"}
repeat with anItem in utilityItems
make new file at utilityFolder with properties {name:anItem}
end repeat
repeat with anItem in managerItems
make new file at managerFolder with properties {name:anItem}
end repeat
repeat with anItem in listenerItems
make new file at listenerFolder with properties {name:anItem}
end repeat
end tell
end
Thanks for reading.
Jort
PS: If you know another file extension I can add like .class .txt .pkg etc. then please give a comment to this question with that other file extension. (Because I still want a bigger fake library to be generated and with real existing file names it looks better :P)
Not really an answer, but an improvement.
This is a shorter version of your script using lists and repeat loops. Instead of an error branch repeating the whole code to create the structure it checks if the folder mods exists and delete it if necessary before creating the items.
property modsItems : {"Render.plist", "KleurInstellingen.txt", "Vrienden.txt", "Vijanden.txt", "Rust-Python-Embed.pyth", ¬
"Std__PATH.plist", "CargoRun.sett", "Rust-Package.fix", "Rust-Package-Auto.app", ¬
"Rust-PKG.pkg", "Project-Layout.jar", "Run-EXP.gib", "Downloaded.txt", "Enttree.jar", "Radar.jar"}
property displayItems : {"Commons-io-2.4.jar", "commons-io-2.4.jar.sha", "Lastlog.log.gz", "Lastlog2.log.gz", ¬
"windows.pkg", "ModuleSettings.pkg", "ModuleStates.pkg", "ColorBinds.pkg", "ModsReturn.pkg", "Textures_1.png", "Textures_2.png", "Textures_3.png"}
property jortpepeItems : {"Rust.class", "Rust$1.class"}
property utilityItems : {"Layout.class", "XYZ__Layout.class"}
property managerItems : {"Restrictions.class", "MAX__DELAY.class"}
property listenerItems : {"OtherEvents.class", "PlayerEvents.class", "PlayerEvents$1.class", ¬
"PlayerChat.class", "PlayerChat$1.class"}
set chosenfolder to choose folder with prompt "Choose the Game folder to install the mods."
tell application "Finder"
if exists folder "mods" of chosenfolder then delete folder "mods" of chosenfolder
set modsFolder to make new folder at chosenfolder with properties {name:"mods"}
repeat with anItem in modsItems
make new file at modsFolder with properties {name:anItem}
end repeat
set displayFolder to make new folder at modsFolder with properties {name:"Display"}
repeat with anItem in displayItems
make new file at displayFolder with properties {name:anItem}
end repeat
set jortpepeFolder to make new folder at modsFolder with properties {name:"jortpepe"}
repeat with anItem in jortpepeItems
make new file at jortpepeFolder with properties {name:anItem}
end repeat
set utilityFolder to make new folder at jortpepeFolder with properties {name:"Utilities"}
set managerFolder to make new folder at jortpepeFolder with properties {name:"Managers"}
set listenerFolder to make new folder at jortpepeFolder with properties {name:"Listeners"}
repeat with anItem in utilityItems
make new file at utilityFolder with properties {name:anItem}
end repeat
repeat with anItem in managerItems
make new file at managerFolder with properties {name:anItem}
end repeat
repeat with anItem in listenerItems
make new file at listenerFolder with properties {name:anItem}
end repeat
end tell
I already got the answer. In the script you'll see this code:
set thisfolder6 to make new file at thisfolder3 with properties {name:"Listeners"}
And I forgot to change the new file to new folder (I just did copy, paste, copy, paste, etc.)
So now the fixed code is:
set thisfolder6 to make new folder at thisfolder3 with properties {name:"Listeners"}
(I also changed this for the other rows)

create subfolder in subfolder and move file

i am new to this and am looking for help: i have a piece of code that is working but wanna extend it and can't make it work so far:
i have this to create a project folder in a chosen location, with two folders and two subfolders:
`set JobName to text returned of (display dialog "Please enter Project Name:" default answer "New_Project")
set loc to choose folder "Choose Parent Folder Location"
tell application "Finder"
set newfo to make new folder at loc with properties {name:JobName}
set targetFolder to make new folder at newfo with properties {name:"01_folder"}
make new folder at targetFolder with properties {name:"01_sub1"}
set targetFolder to make new folder at newfo with properties {name:"02_folder"}
make new folder at targetFolder with properties {name:"02_sub1"}
end tell`
i have two questions:
1. how can I create a subfolder in subfolder {name:"01_sub1"}?
2. how can I move a file into one of the folder that are being created?
(I know how to move to a fixed path but not one that is being chosen in the process of the same script)
i would be super thankful for any hints!
spoke
You are already doing it in your other lines so I don't understand why you have problems. You have this line:
make new folder at targetFolder with properties {name:"01_sub1"}
Just change that to capture the folder variable like this...
set subFolder1 to make new folder at targetFolder with properties {name:"01_sub1"}
Then you can put a sub folder into that with...
set anotherSubFolder to make new folder at subFolder1 with properties {name:"another subfolder"}
Note you'll also capture that folder variable so all you need to do to move a file to it is...
move someFile to anotherSubFolder
All I'm showing you is using the same logic you've already used elsewhere in the code.

Create folder structure on a networked drive

I'm trying to create a folder structure using applescript. This code works great locally, but when I try it on a networked drive it returns "Can't get <> "test" of application "Finder".
This is the code I have
tell application "Finder"
set campaignName to text returned of (display dialog "Please enter Campaign Name:" default answer "Campaign Name")
tell application "Finder" to set currentDirectory to target of Finder window 1
make new folder at currentDirectory with properties {name:campaignName}
make new folder at folder campaignName with properties {name:"a-creative"}
make new folder at folder "a-creative" of folder campaignName with properties {name:"a-assets-from-client"}
make new folder at folder "a-creative" of folder campaignName with properties {name:"b-art-director-files"}
make new folder at folder "a-creative" of folder campaignName with properties {name:"c-for-production"}
make new folder at folder "a-creative" of folder campaignName with properties {name:"d-developer-notes"}
make new folder at folder "b-art-director-files" of folder "a-creative" of folder campaignName with properties {name:"PSD"}
make new folder at folder "b-art-director-files" of folder "a-creative" of folder campaignName with properties {name:"storyboard"}
end tell
I'm hoping someone can help me figure out what I'm doing wrong here. I'm new to using applescript.
Thank you!
Try it this way:
tell application "Finder"
set campaignName to text returned of (display dialog "Please enter Campaign Name:" default answer "Campaign Name")
tell application "Finder" to set currentDirectory to target of Finder window 1
set projectFolderRoot to make new folder at currentDirectory with properties {name:campaignName}
set aCreativeRoot to make new folder at projectFolderRoot with properties {name:"a-creative"}
set bArtDirectorRoot to make new folder at aCreativeRoot with properties {name:"b-art-director-files"}
make new folder at aCreativeRoot with properties {name:"c-for-production"}
make new folder at aCreativeRoot with properties {name:"d-developer-notes"}
make new folder at bArtDirectorRoot with properties {name:"PSD"}
make new folder at bArtDirectorRoot with properties {name:"storyboard"}
end tell

Applescript: make new folder structure if it doesn't exist

I've been searching through message boards trying to figure out how to execute this script. Essentially the goal is to be able to run this script while inside a folder and if certain folders do not exist, these folders would then be created. If they do already exist, nothing happens. Here's what I've cobbled together so far:
property archivesFolder : "Archives"
property imagesFolder : "Images"
property proofreadFolder : "Proofreading"
property proofFolder : "Proofs"
property sourceFolder : "Source"
try
tell application "Finder" to set theLocation to (folder of the front window as alias)
end try
tell application "Finder"
if (exists folder archivesFolder) then
(* do nothing *)
else
make new folder at theLocation with properties {name:archivesFolder}
end if
if (exists folder imagesFolder) then
(* do nothing *)
else
make new folder at theLocation with properties {name:imagesFolder}
end if
if (exists folder proofreadFolder) then
(* do nothing *)
else
make new folder at theLocation with properties {name:proofreadFolder}
end if
if (exists folder proofFolder) then
(* do nothing *)
else
make new folder at theLocation with properties {name:proofFolder}
end if
if (exists folder sourceFolder) then
(* do nothing *)
else
make new folder at theLocation with properties {name:sourceFolder}
end if
end tell
What am I doing wrong ? (forgive my n00b code formatting, at work and can't figure out how to create code blocks) Also, is it possible to this not just on the front window, but on a folder that is just selected? Any help given would be awesome.
I suggest two options (to run the script):
Option 1: Take that code (assuming it does what you plan), and save it as an application (with Script Editor).
Then, just drag that application to your window toolbar (you need to have the toolbar visible). To do that, hold the command key while dragging.
Option 2: Use Butler: http://manytricks.com/butler/
(there is a free version, I don't know your OSX version).
It allows you to define system-wide shortcut keys for applescript scripts.
Create a smart item (applescript); paste the code there, and in the name of the script add the shortcut keys: example: create folder here ⇧⌥⌘N
EDIT:
According to your comment, I understand your problem and I can tell you that you were missing the path (the current folder, in your case theLocation)
So, in every case of if (exists folder archivesFolder) then you need to add the of theLocation like this: if not (exists folder archivesFolder of theLocation) then
Finally, knowing that you won't do any thing if the folder exists, just test the false case.
I tested this code and am posting it here:
property archivesFolder : "Archives"
property imagesFolder : "Images"
property proofreadFolder : "Proofreading"
property proofFolder : "Proofs"
property sourceFolder : "Source"
try
tell application "Finder" to set theLocation to (folder of the front window as alias)
end try
tell application "Finder"
if not (exists folder archivesFolder of theLocation) then
make new folder at theLocation with properties {name:archivesFolder}
end if
if not (exists folder imagesFolder of theLocation) then
make new folder at theLocation with properties {name:imagesFolder}
end if
if not (exists folder proofreadFolder of theLocation) then
make new folder at theLocation with properties {name:proofreadFolder}
end if
if not (exists folder proofFolder of theLocation) then
make new folder at theLocation with properties {name:proofFolder}
end if
if not (exists folder sourceFolder of theLocation) then
make new folder at theLocation with properties {name:sourceFolder}
end if
end tell
You can also use a shell script with mkdir, since the option to create intermediate folders won't error if the folder already exists.
# define a list of folders - items will need to be quoted if they contain spaces, etc.
property theFolders : {"Archives", "Images", "ProofReading", "Proofs", "Source"} -- can also nest, e.g. "Inside/One"
try
tell application "Finder" to set targetFolder to (target of the front window) as alias
on error -- no window
set targetFolder to (choose folder)
end try
# build a parameter string from the folder list
set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set {theFolders, AppleScript's text item delimiters} to {theFolders as text, tempTID}
do shell script "cd " & quoted form of POSIX path of targetFolder & "; mkdir -p " & theFolders
Try this:
tell application "Finder"
set theLocation to (target of front window) as string
set folderNames to {"archivesFolder", "imagesFolder", "proofreadFolder", "proofFolder", "sourceFolder"}
repeat with theFolder in folderNames
try
make new folder at theLocation with properties {name:"" & theFolder & ""}
end try
end repeat
end tell
This is a script I did to sort a bunch of medical files into subfolders based on the date of service (explaining the "text 5 thru 14" in the script; the files are named according to a pattern so the script can extract the date of service from the filename). Rather than check if the folder already exists, I just put the make folder instruction in a try block; if the folder already exists, the 'try' fails, but the script continues under the assumption that the folder already exists. Using the 'text' item instead of 'character' returns the extracted string as a single string and not as an array of characters that have to be converted back into a string.
tell application "Finder"
set theDirectory to "Internal Disk:Users:steven:Documents:Vondalee:Medical"
set theFiles to every file in folder theDirectory whose name extension is "pdf"
repeat with eachFile in theFiles
set theFileName to the name of eachFile
set theFolder to text 5 thru 14 of theFileName
try
make new folder at theDirectory with properties {name:theFolder}
end try
move eachFile to folder theFolder of folder theDirectory
end repeat
end tell

Resources