Applescript Copy file to a new folder

℡╲_俬逩灬. 提交于 2019-12-23 07:26:25

问题


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

回答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.




回答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"



回答3:


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



回答4:


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



回答5:


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



回答6:


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



回答7:


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"




回答8:


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/



来源:https://stackoverflow.com/questions/902342/applescript-copy-file-to-a-new-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!