Applescript Copy file to a new folder
64,467
Solution 1
Generally:
tell application "Finder"
make new folder at alias "Macintosh HD:Users:user:Desktop:" with properties {name:"Test Folder 2"}
copy file "Macintosh HD:Users:user:Desktop:Test Folder 1:test.doc" to folder "Macintosh HD:Users:user:Desktop:Test Folder 2"
end tell
You can add variable names that represent POSIX files and paths.
Obviously the colon character (:) is a reserved character for folder- and filenames.
set desktopFolder to "Macintosh HD/Users/user/Desktop/"
set desktopFdrPosix to quoted form of POSIX path of desktopFolder
set newFolderName to "Test Folder 2"
set destinationFdrPosix to quoted form of desktopFdrPosix & POSIX file newFolderName
set sourceFilename to "Test Folder 1/test.doc"
set sourceFnPosix to quoted form of desktopFdrPosix & POSIX file sourceFilename
tell application "Finder"
make new folder at alias desktopFdrPosix with properties {name:newFolderName}
copy file sourceFnPosix to folder destinationFdrPosix
end tell
You may also want to add error checking if the destination folder already exists.
Solution 2
The trick with AppleScript is that moving files is done using aliases.
More realistically it might be easier to make a shell script instead which can be run from AppleScript using do shell script
if you're using Automator or something similar.
#!/bin/sh
fileToBeMoved="$HOME/Desktop/Test Folder 1/test.doc"
newFolderName="Test Folder 2"
mkdir "$newFolderName"
cp -a "$fileToBeMoved" "$newFolderName"
Solution 3
This works for copying to a mounted network volume:
mount volume "afp://compname.local/mountpoint"
tell application "Finder"
duplicate file "MyTextFile.txt" of folder "Documents" of home to disk "mountpoint"
eject "mountpoint"
end tell
Author by
Admin
Updated on October 10, 2020Comments
-
Admin over 3 years
I need to create an AppleSript that will copy specified files from one folder to a newly created folder.
These files need to be specified in the AppleScript editor so something like:
start fileToBeMoved = "Desktop/Test Folder 1/test.doc" newfoldername = "Test Folder 2" make newfolder and name it 'newfoldername' copy 'fileToBeMoved' to 'newfolder' end
-
aayush sharma over 12 yearsIts good to use this method over the applescript ones cause sometimes you might have permission issues and using this way helps then.
-
damianostre about 12 yearsThe downsides of this are: User won't see a progress bar if the copy takes longer. User can't cancel, either, for the same reason. Supporting these operations with a shell command will be a lot harder than simply letting the Finder do it. Downside of Applescripting the Finder, though: If the user chooses to use a replacement "Finder" app, the Applescript operation may fail (not sure about this, probably depends on how well the replacement is written).
-
damianostre about 12 yearsAnother plus for using the Finder: If the destination item exists, the user will be asked to replace or not.
-
nekno about 6 yearsWhat do I need to change to copy to a folder on a mounted SMB file share? I have the folder "Movies" on a share mounted at
/Volumes/Video
. I tried creating aquoted form of POSIX path
forVideo
,Video/Movies
,/Volumes/Video
, and/Volumes/Video/Movies
, for bothcopy file
andduplicate file
, with eitherto folder
orto disk
. None of them worked. All give the error:execution error: Finder got an error: Can't set folder
(ordisk
)"'/Volumes/Video/Movies'" to file "'/Users/me/path/to/file.m4v'". (-10006)