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
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.
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"
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
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
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
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
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"
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/
Related
I am trying to move files from 1 folder to a certain folder if they have a specific name. The listOfFiles are the files to be moved.
I am getting an error cant make Can’t make {\"file1.rtf\", \"file2.rtf\", \"file3.rtf\"} into type integer
I haven't made the condition to check the name yet. first trying to just move the files.
set listOfFiles to {"file1.rtf", "file2.rtf","file3.rtf"}
tell application "Finder"
set sourceFolder to ((path to desktop) & "moveTest1") as string as alias
set goFolder to ((path to desktop) & "moveTest2") as string as alias
set goFiles to the name of every file of sourceFolder
repeat with i from 1 to the count of goFiles
if goFiles = listOfFiles then
move file goFiles to folder goFolder
end if
end repeat
end tell
Thanks!
The main problem is that you can't specify a list of strings (goFiles) with a single file specifier.
Assuming you want to copy all files from folder moveTest1 to folder moveTest2 whose file names are in listOfFiles you can filter those files in one line:
set goFiles to every file of sourceFolder whose name is in listOfFiles
Since the desktop folder is the root folder of the Finder you don't need to specify the desktop folder explicitly.
set listOfFiles to {"file1.rtf", "file2.rtf", "file3.rtf"}
tell application "Finder"
set sourceFolder to folder "moveTest1"
set goFolder to folder "moveTest2"
set goFiles to every file of sourceFolder whose name is in listOfFiles
move goFiles to goFolder
end tell
Actually you can write the move operation in a single line
tell application "Finder" to move (every file of folder "moveTest1" whose name is in listOfFiles) to folder "moveTest2"
I am attempting to create a script or series of scripts to accomplish a semi-complex task. I have already completed the first step, which is to automatically open chrome, visit a site, enter login information and download a .pkg file. What I am looking for is help on making the next part of the script, which is to open the .pkg file from my Downloads folder and install it autonomously. The file name is QuickAdd.pkg, but is renamed as QuickAdd(number).pkg, and hence I need to sort the folder by most recently added. Below is the script that I have so far devised to get the correct file path, but it seems to be very convoluted to me. Any help is greatly appreciated as I am not nearly experienced enough to complete this on my own.
Script v1:
set sourceFolder to (path to downloads folder)
tell application "Finder"
set fileList to (sort (get files of sourceFolder) by creation date)
-- This raises an error if the folder doesn't contain any files
-- Last File is the Most Recently Created --
set theFile to (last item of fileList) as alias
set thePath to POSIX path of theFile
set oProp to (properties of theFile)
set URLstr to URL of oProp
end tell
return URLstr
This works great except that is returns the file path as "file:///Users/wesleycharlap/Downloads/(filename.pkg)", and I cant seem to get any command to recognize the file path as a .pkg and install it. So I assumed the issue was the "file:///" prefix, and thus my second script.
Script v2:
use framework "Foundation"
use scripting additions
set thePath to POSIX path of (path to downloads folder as text)
set sortedList to its filesIn:thePath sortedBy:
(current application's NSURLAddedToDirectoryDateKey)
on filesIn:folderPOSIXPath sortedBy:sortKey
set keysToRequest to {current application's NSURLPathKey, ¬
current application's NSURLIsPackageKey, ¬
current application's NSURLIsDirectoryKey, ¬
sortKey}
set theFolderURL to current application's class "NSURL"'s fileURLWithPath:folderPOSIXPath
set theNSFileManager to current application's NSFileManager's defaultManager()
set listOfNSURLs to (theNSFileManager's contentsOfDirectoryAtURL:theFolderURL ¬
includingPropertiesForKeys:keysToRequest ¬
options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) ¬
|error|:(missing value))
set valuesNSArray to current application's NSMutableArray's array()
repeat with oneNSURL in listOfNSURLs
(valuesNSArray's addObject:(oneNSURL's resourceValuesForKeys:keysToRequest |error|:(missing value)))
end repeat
set theNSPredicate to current application's NSPredicate's predicateWithFormat_("%K == NO OR %K == YES", current application's NSURLIsDirectoryKey, current application's NSURLIsPackageKey)
set valuesNSArray to valuesNSArray's filteredArrayUsingPredicate:theNSPredicate
set theDescriptor to current application's NSSortDescriptor's sortDescriptorWithKey:(sortKey) ascending:true
set theSortedNSArray to valuesNSArray's sortedArrayUsingDescriptors:{theDescriptor}
return (theSortedNSArray's lastObject()'s valueForKey:(current application's NSURLPathKey)) as text
end filesIn:sortedBy:
This version does indeed give me the correct file path "/Users/wesleycharlap/Downloads/(filename).pkg" but I am confused on where to go from here.
Thx for reading
Script v1 can be replaced by
set sourceFolder to (path to downloads folder)
tell application "Finder"
set allFiles to files of sourceFolder whose name extension is "pkg"
if (count allFiles) = 0 then return
set latestFile to last item of (sort allFiles by creation date)
open latestFile
end tell
It considers only .pkg files and opens the most recent one. The next step to automate the install process of the package is not trivial because Installer.app is not scriptable.
I'm trying to copy files and directories from one directory called "Motion Templates" to another directory called "Motion Templates".
tell application "Finder"
set srcPath to ((parent of (path to me) as text) & "Motion Templates")
set dstPath to (((path to movies folder) as text) & "Motion Templates")
set srcFolder to folder srcPath
set dstFolder to folder dstPath
duplicate entire contents of srcFolder to dstFolder with replacing
end tell
The source directory (Motion Templates) contains a subdirectory (Generators) which successfully copies over to the destination "Motion Templates" in terms of matching file paths (from a source "Motion Templates/Generators" to a destination "Motion Templates/Generators"). However, this script also deletes all of the existing subdirectories that existed in "Motion Templates/Generators". How do I go about copying/overwriting files without deleting the other existing files in the tree?
This is far to confusing to explain when you are using the same names. It's like trying to understand Who's on first for the first time. Try using different folder names until you can get it working then worry about what things are called.
To try and help the only thing I can say is if the files have the same exact names and you use the "with replacing" it will overwrite what ever is in the destination folder. If you are saying the source folder is being deleted then you are having an issue where the files are being moved and not duplicated.
How can I make a batch file move files from the directory it is running from? For example code:
**move {***CURRENT BATCH FILE DIRECTORY***}\Programs\myfile.txt**
What do I replace {***CURRENT BATCH FILE DIRECTORY***} with?
When you say "the directory it is running from", I assume you mean the folder that contains the currently executing batch file. If so, then you want %~dp0. The expansion will automatically append the trailing backslash.
move "%~dp0Programs\myfile.txt" "target folder"
Don't the "." (dot) work for you? I mean:
move .\Programs\myfile.txt
This works in Windows, if the current directory is where Programs is.
move "Programs\myfile.txt" "target folder"
I have the code below to set a variable in Applescript for the path to the iTunes Music Folder:
set username to text returned of (display dialog "RingtoneDude" default answer "Enter your path to your iTunes Ringtones folder here. e.g. /Users/David/Music/iTunes/iTunes Music/Ringtones" buttons {"Confirm", "Cancel"} default button 1)
And then I have the code to call the variable username to copy a file
tell application "Finder"
copy file theCopy to username
end tell
but the file theCopy which is on the desktop (theCopy is a variable) does not move to the folder.
Please help.
I believe you've misunderstood the copy command. The copy command is used for transferring the contents of one variable into another variable. What you need to use is the duplicate command, which is used for copying files to a specified location. Its syntax is as follows:
duplicate [alias] to [alias]
[replacing boolean] --If true, replaces files in the destination with the same name
Having said that, your new code, in conjunction with Uriah Carpenter's answer, should look something like this:
set myRingtoneFolder to choose folder with prompt "Select your iTunes Ringtone folder:"
tell application "Finder" to duplicate theCopy to myRingtoneFolder
I would suggest you use choose folder which returns an alias.
To make some text into an alias object use set myAliasPath to myTextPath as alias.
For more detailed information see Aliases and Files in the AppleScript documentation.