user111595
user111595

Reputation:

Applescript Copy file to a new folder

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

Upvotes: 11

Views: 69273

Answers (8)

Chealion
Chealion

Reputation: 1281

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"

Upvotes: 5

SashaZd
SashaZd

Reputation: 3319

If it's a sync you're looking for you can run the following shell script in your AppleScript:

rsync -a /Users/username/folderToBeCopiedFrom /Volumes/folderToBeCopiedTo/

Upvotes: 0

Midverse Engineer
Midverse Engineer

Reputation: 991

I used Chealions shell script solution. Don't forget to make your script file executable with: sudo chmod u+x scriptFilename

Also remove the space between the = sign in the variable assignments. Wouldn't work with the spaces, for example: newFolderName="Test Folder 2"

Upvotes: 0

Michael Sanders
Michael Sanders

Reputation: 107

The simple way to do it is as below says

set home_path to path to home folder as string
set work_folder to alias (home_path & "AutomationAppleScript:ScreenShots:")
tell application "Finder"
    duplicate every file of work_folder to folder "Archive" of home
end tell

Upvotes: 0

Lri
Lri

Reputation: 27613

tell application "Finder"
    make new folder at desktop with properties {name:"folder"}
    duplicate POSIX file "/usr/share/doc/bash/bash.html" to result
end tell

POSIX file ((system attribute "HOME") & "/Documents" as text)
tell application "Finder" to result

tell application "Finder" to duplicate (get selection) to desktop replacing yes

-- these are documented in the dictionary of System Events
path to home folder
POSIX path of (path to documents folder)
path to library folder from user domain
path to desktop folder as text

-- getting files as alias list is faster
tell application "Finder"
    files of entire contents of (path to preferences folder) as alias list
end tell

Upvotes: 0

Sumanjit Saha
Sumanjit Saha

Reputation:

set home_path to path to home folder as string
set work_folder to alias (home_path & "AutomationAppleScript:ScreenShots:")
tell application "Finder"
    duplicate every file of work_folder to folder "Archive" of home
end tell

Upvotes: 3

Wilersh
Wilersh

Reputation: 85

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

Upvotes: 1

Alex Reynolds
Alex Reynolds

Reputation: 96937

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.

Upvotes: 13

Related Questions