Applescript - duplicate file to folder - applescript

(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

Related

Trouble with script moving files to a newly created folder in Applescript

I am having trouble with what I think is a pretty straightforward task, but cannot seem to get my script to work correctly. I have found a lot of help through the forums with regards to the individual routines used, but it still seems to fail.
In short, what I would like to do is monitor a folder for new files being added. Once a batch of files are added (every other day or so), it will create a folder in another directory with the new folder name being the current date, move those files to the new directory, and then execute a simple bash script which uses the new directory name as an argument.
My script compiles ok, but once files are added it only creates the new folder and nothing else. I appreciate any help.
property the_sep : "-"
on adding folder items to my_folder after receiving the_files
tell application "Finder"
(* First create a new folder with name of folder = current date *)
set the_path to (folder "qa" of folder "Documents" of folder "ehmlab" of folder "Users" of disk "Macintosh HD")
set the_name to (item 1 of my myDate())
set the_name to (the_name & the_sep & item 2 of my myDate())
set the_name to (the_name & the_sep & item 3 of my myDate())
make folder at the_path with properties {name:the_name}
set newDir to the_path & the_name
end tell
(* Next, move the newly added files to the source into the newly created date folder *)
repeat with i from 1 to number of items in the_files
tell application "Finder"
try
set this_file to (item i of the_files)
move file this_file to folder newDir
end try
end tell
end repeat
do shell script "qc.sh " & newDir
end adding folder items to
on myDate()
set myYear to "" & year of (current date)
set myMth to text -2 thru -1 of ("0" & (month of (current date)) * 1)
set myDay to text -2 thru -1 of ("0" & day of (current date))
return {myYear, myMth, myDay}
end myDate
The failure reason are different path styles.
AppleScript uses HFS paths (colon separated).
UNIX uses POSIX paths (slash separated).
The solution is to coerce the HFS path string to POSIX path
do shell script "qc.sh " & quoted form of POSIX path of newDir
This is a shorter version of the script using the shell also for the time stamp and for creating the directory.
on adding folder items to my_folder after receiving the_files
(* Next, move the newly added files to the source into the newly created date folder,
"path to documents" is a relative path to the documents folder of the current user *)
set baseFolder to (path to documents folder as text) & "qa:"
set timeStamp to do shell script "date +%Y-%m-%d"
set newDir to baseFolder & timeStamp
do shell script "mkdir -p " & quoted form of POSIX path of newDir
(* Next, move the newly added files to the source into the newly created date folder *)
repeat with aFile in the_files
tell application "Finder"
try
move aFile to folder newDir
end try
end tell
end repeat
do shell script "qc.sh " & quoted form of POSIX path of newDir
end adding folder items to

Burning files for a video to backup DVD

I'm working on an AppleScript to burn the files for a church sermon to a DVD for backup. A word about our process.
We have a Sony digital camcorder that we use to video the service. I download those files into iMovie, create an iMovie project, and stitch the pieces together. I add an opening title & ending credits and I share it to the Media Browser at two resolutions. After that, I upload the smaller resolution file to our church's website. I then create an iDVD project using the larger resolution file.
I've got templates for the iMovie project with the opening title & the closing credits in it already, and a template for the iDVD project, and another template for printing out DVD labels. I've already written an AppleScript which duplicates these templates for a specific week. That's working fine.
I've got a lot of work done on a second script to create a Burn Folder in my Desktop folder and create folders in it & create aliases / shortcuts to the original files. But, given that it's video we're copying here, I need to make sure that the minimum amount of data required is burned to the disk.
Specifically, the iMovie Events folder for the week's raw video has two folders created by iMovie in it that I don't want to burn to the DVD. One is a cache & the other has thumbnails. These should be recreated if I have to restore the backup for any reason when I open iMovie.
So I only want to copy the files in the event folder that are not these two folders. The folders are called iMovie Movie Cacheand iMovie Thumbnails.
How do I exclude these two files? The code I've got follows.
-- Script to copy the iMovie project, the iDVD project, and the DVD Label
-- files for a particular week's sermon into a burn folder and then burn it.
property iMovieEvents : alias ((path to movies folder as text) & "iMovie Events.localized:")
property iMovieProjects : alias ((path to movies folder as text) & "iMovie Projects.localized:")
property iMovieSermonsPath : alias ((iMovieProjects as text) & "Sermons:")
property sermonDocumentsPath : alias ((path to documents folder as text) & "Sermons:")
global burnFolder
global thisSermonsPath
global thisSermonsDvdLabels
global thisSermonsdvdproj
global thisSermonsiMovieEvents
global thisSermonsiMovieProject
global sermonCode
to copySermonFiles()
-- Start a conversation with the Finder
tell application "Finder"
-- Create a folder in the burn folder for the iMovie Events
set theFolder to my createFolder(burnFolder, "iMovie Events.localized")
-- Give this operation 5 mintues to complete
with timeout of (5 * 60) seconds
-- Copy the Sermon's iMovie Events folder to the Burn Folder.
-- Need to exclude the iMovie Movie Cache & iMovie Thumbnails folders here
make new alias at theFolder to folder thisSermonsiMovieEvents
end timeout
-- Create a folder in the burn folder for the Sermon's iMovie Project.
set theFolder to my createFolder(burnFolder, "iMovie Projects.localized")
with timeout of (5 * 60) seconds
-- Copy the Sermon's iMovie Project file to the Burn Folder
make new alias at theFolder to file thisSermonsiMovieProject
end timeout
-- Create a folder in the burn folder for the Sermon's documents
set theFolder to my createFolder(burnFolder, "Documents")
-- Create a folder in the burn folder's Documents folder for the sermon
set theFolder to my createFolder(theFolder, sermonCode)
-- Copy the Sermon's iDVD Project and DVD Labels files to the Burn Folder
make new alias at theFolder to thisSermonsDvdLabels
make new alias at theFolder to thisSermonsdvdproj
end tell
end copySermonFiles
-- Helper method that creates a new folder named theName in the dst folder.
to createFolder(dst, theName)
-- Create a string that contains the path to the folder we're going to create
set thePath to (dst as text) & theName
-- Start a conversation with the Finder
tell application "Finder"
-- Does the folder exist already?
if not (exists thePath) then
return make new folder at dst with properties {name:theName}
else
return alias thePath
end if
end tell
end createFolder
to createBurnFolder()
-- Create the name of the Burn Folder
set burnFolderName to sermonCode & ".fpbf"
set desktopPath to (path to desktop) as text
-- Compute the path to the Sermon's folder in the Documents:Sermons folder.
set burnFolderPath to desktopPath & burnFolderName
-- Start a conversation with the Finder
tell application "Finder"
-- Does the burn folder exist?
if not (exists burnFolderPath) then
-- Create the burn folder
set burnFolder to make new folder at desktopPath with properties {name:burnFolderName}
else
set burnFolder to folder burnFolderPath
end if
end tell
end createBurnFolder
to getThisSermonsFiles()
-- Ask the user to pick the Sermon's iMovie Project file.
set thisSermonsiMovieProject to choose file with prompt "Please select the Sermon's iMovie Project:" default location iMovieSermonsPath
-- Extract the sermon code from the Sermon's iMovie Project file name.
set fileext to ".rcproject"
set fileName to (name of (info for thisSermonsiMovieProject))
set sermonCode to text 1 thru ((length of fileName) - (length of fileext)) of fileName
-- Build the path to the Sermon's iMovie Events folder
set thisSermonsiMovieEvents to ((iMovieEvents as text) & sermonCode & ":")
-- Build the path to the Sermon's DVD & DVD Labels files.
set thisSermonsPath to (sermonDocumentsPath as text) & sermonCode & ":"
-- Build the path to this Sermon's DVD Labels file.
set thisSermonsDvdLabels to alias (thisSermonsPath & sermonCode & ".cndx")
-- Build the path to this sermon's iDVD Project file.
set thisSermonsdvdproj to alias (thisSermonsPath & sermonCode & ".dvdproj")
end getThisSermonsFiles
on run
-- Get the sermon code from the user
getThisSermonsFiles()
-- Create the Burn folder
createBurnFolder()
-- Copy the files into the burn folder.
copySermonFiles()
end run
The Finder has a "whose" claus which makes filtering a list of Finder items easy. Thus we can use it to exclude things. You can also refer to files or folders as "items" to include both in one phrase. So if you want to get a reference to all files and folders from a folder, excluding some items by their name, then something like this works...
set itemNamesToExclude to {"iMovie Movie Cache", "iMovie Thumbnails"}
tell application "Finder"
set itemsToMove to items of folder thisSermonsiMovieEvents whose name is not in itemNamesToExclude
-- move itemsToMove somewhere
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: Create folders/subfolders and move multiple files

I have an Applescript question that is much more complex than I can construct. I have been searching for the past couple of days, and I cannot find any script like this, nor can I find enough information to piece one together with my limited knowledge.
I have multiple files with structured names. Each file has the following name structure:
ttu_collectionname_000001.pdf
ttu_collectionname_000002.mp3
ttu_collectionname_000003.pdf ... etc. (Each of these files are of varying file types.)
There is also a csv metadata file associated with each of the original files.
ttu_collectionname_000001.csv
ttu_collectionname_000002.csv
ttu_collectionname_000003.csv ... etc. (Each of these files are csv files.)
I need to create a folder based on the name of the file with sub and sub-subfolders. Each top-level folder name will be unique in the number sequence. Each sub and sub-subfolder name will be the same for each top-level folder.
The folder structure should look like this:
ttu_collectionname_000001
content
archive
display
metadata
archive
display
ttu_collectionname_000002
content
archive
display
metadata
archive
display
I then need to move the each file to a particular sub-subfolder.
The file ttu_collectionname_000001.pdf would be moved to the ttu_collectionname_000001/content/display folder.
The file ttu_collectionname_000001.csv would be moved to the ttu_collectionname_000001/metadata/display folder.
Try:
set myFolder to "Mac OS X:Users:stark:Main Folder"
tell application "Finder" to set myFiles to folder myFolder's files as alias list
repeat with aFile in myFiles
tell application "System Events" to set {fileName, fileExt} to {name, name extension} of aFile
set baseName to text 1 thru ((get offset of "." & fileExt in fileName) - 1) of fileName
do shell script "mkdir -p " & (quoted form of (POSIX path of myFolder)) & "/" & baseName & "/{\"content\",\"metadata\"}/{\"display\",\"archive\"}"
tell application "System Events"
if fileExt is "pdf" then move aFile to (myFolder & ":" & baseName & ":content:display" as text)
if fileExt is "csv" then move aFile to (myFolder & ":" & baseName & ":metadata:display" as text)
end tell
end repeat
Assuming your files are in the same folder,
this AppleScript:
set pathToFolderOfTTUFiles to (path to the desktop as text) & "TTU:"
tell application "Finder"
set theFiles to every item of folder pathToFolderOfTTUFiles whose name extension is not "csv" and kind is not "Folder"
repeat with theFile in theFiles
set lengthOfExtension to (length of (theFile's name extension as text)) + 1
set fileNameWithoutExtension to text 1 through -(lengthOfExtension + 1) of (theFile's name as text)
set theFolder to make new folder at folder pathToFolderOfTTUFiles with properties {name:fileNameWithoutExtension}
set theContentFolder to make new folder at theFolder with properties {name:"content"}
make new folder at theContentFolder with properties {name:"archive"}
set theContentDisplayFolder to make new folder at theContentFolder with properties {name:"display"}
set theMetadataFolder to make new folder at theFolder with properties {name:"metadata"}
make new folder at theMetadataFolder with properties {name:"archive"}
set theMetadataDisplayFolder to make new folder at theMetadataFolder with properties {name:"display"}
move theFile to theContentDisplayFolder
set pathToCSV to pathToFolderOfTTUFiles & fileNameWithoutExtension & ".csv"
if exists pathToCSV then move pathToCSV to theMetadataDisplayFolder
end repeat
end tell
creates this:

how to use applescript to move files to new folder by extension, keeping subfolder name

Here's what I'm trying to do.
I've got a file structure that contains photos in both JPG and RAW formats. It's a folder called "Photos" with subfolders by date. I'd like to copy just the RAW photos to a new folder, "Photos RAW", but keep the structure by date taken/created.
I can copy just the files using automator or applescript a directory to a new one, but how do I walk the directory tree using applescript so I cover all the subfolders?
Try this. You'll see I used "entire contents" to get the files in the subfolders too.
set extensionToFind to "raw"
set topLevelFolder to (choose folder) as text
set pathCount to count of topLevelFolder
tell application "Finder"
-- get the files
set rawFiles to files of entire contents of folder topLevelFolder whose name extension is extensionToFind
if rawFiles is {} then return
-- setup the folder where the files will be moved
set rawFolder to ((container of folder topLevelFolder) as text) & "Photos_Raw:"
do shell script "mkdir -p " & quoted form of POSIX path of rawFolder
repeat with aFile in rawFiles
set aFileContainer to (container of aFile) as text
if topLevelFolder is equal to aFileContainer then
-- here the file is at the top level folder
set newPath to rawFolder
else
-- here we calculate the new path and make sure the folder structure is in place
set thisFile to aFile as text
set subFolderPath to text (pathCount + 1) thru -((count of (get name of aFile)) + 1) of thisFile
set newPath to rawFolder & subFolderPath
do shell script "mkdir -p " & quoted form of POSIX path of newPath
end if
move aFile to folder newPath
end repeat
end tell

Resources