macOS 13.1 (22C65) - AppleScript to copy local Dropbox folder files - macos

Can you review this code and tell me what I'm doing wrong. I keep getting
(Expected “,” but found identifier)
set baseFolder to (path to home folder as text) & "Dropbox/My Documents/GreenStripeMedia"
set projectName to "NewProject"
if not (exists folder baseFolder) then
display dialog "The base folder doesn't exist." buttons {"OK"} default button 1 with icon caution
return
end if
if not (folder baseFolder is writable) then
display dialog "You don't have write permissions to the base folder." buttons {"OK"} default button 1 with icon caution
return
end if
if (exists folder (baseFolder & "/" & projectName)) then
display dialog "A folder with that name already exists in the base folder." buttons {"OK"} default button 1 with icon caution
return
end if
set newProjectFolder to baseFolder & "/" & projectName
make new folder at baseFolder with properties {name:projectName}
set masterFolder to baseFolder & "/Master"
tell application "Finder"
set masterFiles to files of masterFolder
repeat with aFile in masterFiles
duplicate aFile to newProjectFolder
end repeat
end tell
Here's what it should all do
Creates a folder in the specified directory "$HOME/Dropbox/My Documents/GreenStripeMedia" with the name "Project Name".
Copies a file named "Content Template.docx" from "$HOME/Dropbox/My Documents/GreenStripeMedia/Master" and renames it to "Project Name Content.docx" in the new directory.
Copies a file named "Logins Template.docx" from "$HOME/Dropbox/My Documents/GreenStripeMedia/Master" and renames it to "Project Name Logins.docx" in the new directory.
Creates an empty directory in "$HOME/Dropbox/My Documents/GreenStripeMedia/Project Name" named "100 Logos".
Also check if the base folder is writable, and displays an error message if it's not.

Related

Applescript/Finder - how to check if a variable folder exists within a fixed folder

I'm trying to get the script to check if there is a variable folder name, eg. "folder x", present from within a folder that has folders a-z for example?
The rest of the script works fine but the problem is that no matter what I've done so far I only get "yes" as the result of this line if exists folder theTitleCV of alias "Comics:" then.
My script so far:
set theTitleCV to "Folder X"
set theTitle to "Folder X-52"
--Creating the folder if it doesn't exists
tell application "Finder"
if exists folder theTitleCV of alias "Comics:" then
if theTitleCV is equal to theTitle then
make new folder of alias "Comics:" with properties {name:theTitleCV}
else
set theTitleResult to ((display dialog "Title Options" buttons {theTitleCV, theTitle, "Cancel"} default button 2))
set theButton to button returned of theTitleResult
make new folder of alias "Comics:" with properties {name:theButton}
end if
else
return "Yes"
end if
end tell
Thanks in advance for any help that is offered.
P.S. If this type of question has been asked and answered before, please just point me in the direction as to what to search for. Thanks :)
thanks to everyone who helped and especially to vadian for spotting the obvious mistake.
I have now fixed the script and have added a display dialog to go with it.
set theTitleCV to "Folder X"
set theTitle to "Folder X-52"
--Creating the folder if it doesn't exist
tell application "Finder"
if not (exists folder theTitleCV of disk "Comics") then
if theTitleCV is equal to theTitle then
make new folder of disk "Comics" with properties {name:theTitleCV}
else
set theTitleResult to ((display dialog "Title Options" buttons {theTitleCV, theTitle} default button 2 with icon ":System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:GenericFolderIcon.icns" as alias))
set theButton to button returned of theTitleResult
make new folder of disk "Comics" with properties {name:theButton}
end if
else
tell application "Finder" to activate
return display dialog "" & return & "Thank you for checking," & return & "but this folder already exists." with title "Folder Already Exists" buttons {"OK"} default button 1 with icon ":System:Library:CoreServices:CoreTypes.bundle:Contents:Resources:AlertNoteIcon.icns" as alias
end if
end tell

Use Applescript to open a file under a specified folder

Say I have a bunch of folders with similar subpaths:
Folder 1
Do
Re
Mi
<Files>
Folder 2
Do
Re
Mi
<Different Files>
I know I can use tell application Finder to open "Macintosh HD:Users: ... to open a folder. Is there any way to queue a popup to select which of Folder 1 or Folder 2 I would like to enter, then input it into the above command? For instance, if I select Folder 1, it would go to ...Folder 1/Do/Re/Mi, whereas if I select Folder 2, it would go to ...Folder 2/Do/Re/Mi.
One thing I tried is combining do shell script with concatenate to get do shell script "open " & variable & "/Do/Re/Mi/", but the code fails if the file name is more than one word.
Set a base folder
Retrieve all folder names of the base folder
Create a list
Choose an item
Concatenate the path and open the folder
For example (set baseFolder to the alias of the parent folder of Folder 1 and Folder 2)
set baseFolder to path to home folder
set folderList to {}
tell application "Finder" to set allFolders to name of folders of baseFolder
repeat with i from 1 to (count allFolders)
set end of folderList to (i as text) & space & item i of allFolders
end repeat
set chosenItem to choose from list folderList
if chosenItem is false then return
set chosenIndex to word 1 of item 1 of chosenItem
set selectedFolder to item chosenIndex of allFolders
set destinationFolder to (baseFolder as text) & selectedFolder & ":Do:Re:Mi:"
tell application "Finder"
if exists folder destinationFolder then
open folder destinationFolder
else
display dialog "Sorry, the folder does not exist"
end if
end tell

Create Folders 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

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

Automator/Applescript File Sorting and Moving

I was wondering if there was a way in Automator to use Applescript code to get the extension of a file, and if it equals a specific extension (e.g. .pdf or .rtf) move to a specific folder for that extension (e.g if (extension == pdf) { move to folder "~/PDF Files" } else if (extension == rtf) { move to folder "~/Rich Text Files" })
Here's an applescript. Since your request was simple I just wrote it for you. Note how I get the file extension with the subroutine "getNameAndExtension(F)". Normally you can get the file extension from the Finder (called name extension) but I've found that the Finder is not always reliable so I always use that subroutine. That subroutine has always been reliable.
set homeFolder to path to home folder as text
set rtfFolderName to "Rich Text Files"
set pdfFolderName to "PDF Files"
-- choose the files
set theFiles to choose file with prompt "Choose RTF or PDF files to move into your home folder" with multiple selections allowed
-- make sure the folders exist
tell application "Finder"
if not (exists folder (homeFolder & rtfFolderName)) then
make new folder at folder homeFolder with properties {name:rtfFolderName}
end if
if not (exists folder (homeFolder & pdfFolderName)) then
make new folder at folder homeFolder with properties {name:pdfFolderName}
end if
end tell
-- move the files
repeat with aFile in theFiles
set fileExtension to item 2 of getNameAndExtension(aFile)
if fileExtension is "rtf" then
tell application "Finder"
move aFile to folder (homeFolder & rtfFolderName)
end tell
else if fileExtension is "pdf" then
tell application "Finder"
move aFile to folder (homeFolder & pdfFolderName)
end tell
end if
end repeat
(*=============== SUBROUTINES ===============*)
on getNameAndExtension(F)
set F to F as Unicode text
set {name:Nm, name extension:Ex} to info for file F without size
if Ex is missing value then set Ex to ""
if Ex is not "" then
set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
end if
return {Nm, Ex}
end getNameAndExtension
I'm not at my Apple right now, so I can't play around with Automator and figure it out. But if you could do this by switching finder to list view, sort by type, and then select blocks of files of the same type and drag them into the right folder.

Resources