I am trying to write an Apple Script that will duplicate two files from one folder to another. I tried to different methods but I keep getting an error.
Method 1:
tell application "Finder"
set home_path to home as text
set framework to alias (home_path & "Library:Developer:Xcode:DerivedData:ReaderKit-cusaxqpwrepxbfcmixaiszkolkkv:Build:Products:Debug-iphoneos:ReaderKit.framework")
set bundle to alias (home_path & "Library:Developer:Xcode:DerivedData:ReaderKit-cusaxqpwrepxbfcmixaiszkolkkv:Build:Products:Debug-iphoneos:ReaderKit.framework")
set destination to home_path & "Documents:WillowTree:BIA-Kelsey:BIA-Kelsey:ThirdParty"
duplicate file framework to folder destination with replacing
duplicate file bundle to folder destination with replacing
end tell
Method 2
tell application "Finder"
set framework to "Macintosh HD:Users:nayef:Library:Developer:Xcode:DerivedData:ReaderKit-cusaxqpwrepxbfcmixaiszkolkkv:Build:Products:Debug-iphoneos:ReaderKit.framework"
set bundle to "Macintosh HD:Users:nayef:Library:Developer:Xcode:DerivedData:ReaderKit-cusaxqpwrepxbfcmixaiszkolkkv:Build:Products:Debug-iphoneos:ReaderKit.bundle"
set destination to "Macintosh HD:Users:User:nayef:Documents:WillowTree:BIA-Kelsey:BIA-Kelsey:ThirdParty"
duplicate file framework to folder destination with replacing
duplicate file bundle to folder destination with replacing
end tell
The Error
error "Finder got an error: Can’t set folder \"Macintosh HD:Users:User:nayef:Documents:WillowTree:BIA-Kelsey:BIA-Kelsey:ThirdParty\" to file \"Macintosh HD:Users:nayef:Library:Developer:Xcode:DerivedData:ReaderKit-cusaxqpwrepxbfcmixaiszkolkkv:Build:Products:Debug-iphoneos:ReaderKit.framework\"." number -10006 from folder "Macintosh HD:Users:User:nayef:Documents:WillowTree:BIA-Kelsey:BIA-Kelsey:ThirdParty"
What's wrong with my script?
This is because "ReaderKit.framework" is not a file, use folder, item or alias
duplicate folder framework to folder destination with replacing
Related
I'm not a newbie to (or an expert on) scripting and programming, but after a few hours, I am stumped at how to copy a file (after having googled and tried various things for a few hours) using AppleScript in this situation.
I want to move a file from the most recently modified folder one level up. I'm trying to move the file:
/Users/tsg/my downloads folder/newest folder/pdfs/target1.pdf
to
/Users/tsg/my downloads folder/newest folder/target1.pdf
Where "newest folder" is not a constant name, but will change as I download newer folders.
When I run the code below,
tell application "Finder"
set downloadFolder to POSIX file "/Users/tsg/my downloads folder" as alias
set latestModifiedFolder to (downloadFolder & (name of last item of (sort every folder of downloadFolder by modification date)))
set targetFolder to latestModifiedFolder (* Is this necessary? *)
set pdfFolder to (targetFolder & ":pdfs:")
set pdfFile to (pdfFolder & ("target1.pdf"))
move pdfFile to targetFolder
end tell
I get the error
"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} into type folder." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} to folder
If I add the modifier folder: "move pdfFile to folder targetFolder" for the last line before "end tell", I get
"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} into type integer." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder"} to integer"
If I add the modifier file "move file pdfFile to folder targetFolder", I get
"Finder got an error: Can’t make {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"} into type integer." number -1700 from {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"} to integer
If I remove the "move" line altogether, I get the result:
{alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:", "target1.pdf"}
I would actually like to find the file based on a search term, declared as a property (Property searchWord : {"target"}), rather than hard coded, but I couldn't get that to work either.
Code:
set pdfFile to first file of pdfFolder whose name contains searchWord
Error:
"Can’t get file 1 of {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:"}." number -1728 from file 1 of {alias "Macintosh HD:Users:tsg:my downloads folder:", "newest folder", ":pdfs:"}
There may be more than one pdf containing the word "target", and I'd actually like to move all files containing the search word up one level, and then rename them pdfs.one.pdf and pdfs.two.pdf.
Thanks in advance for any help.
The errors occur because you try to combine multiple incompatible classes.
When dealing with the Finder the Finder specifier syntax
file "foo" of folder "bar" of folder "baz" of startup disk
is a reasonable way to avoid those cannot-do-this errors
tell application "Finder"
set downloadFolder to folder "my downloads folder" of home
set folderName to name of last item of (sort every folder of downloadFolder by modification date)
set targetFolder to folder folderName of downloadFolder
set pdfFolder to folder "pdfs" of targetFolder
set pdfFile to file "target1.pdf" of pdfFolder
move pdfFile to targetFolder
end tell
or to move all files containing "target" in the file name
tell application "Finder"
set downloadFolder to folder "my downloads folder" of home
set folderName to name of last item of (sort every folder of downloadFolder by modification date)
set targetFolder to folder folderName of downloadFolder
set pdfFolder to folder "pdfs" of targetFolder
set pdfFiles to files of pdfFolder whose name contains "target"
move pdfFiles to targetFolder
end tell
Using Automator, I created a "Folder Action" to move files of certain types (i.e. "Kind IS Movie") from folder "FolderA" to folder "FolderB" as soon as they are added to folder "FolderA". It works fine except that it doesn't move files from subfolders (e.g. doesn't work if the movie files are added to "FolderA/SubA/"), and apparently there's no option on Automator to make it work on the subfolders.
Does anyone have any idea about how to either change that on Automator or how could I create a script for that? Thanks!
This following AppleScript folder action code should give you a general idea of how to accomplish what you are looking for. When a file or folder is added to a folder that this folder action is attached to, it will search all folders and sub folders for files (I used QuickTime movie as the kind of file... But you can change that to whatever you want) then those files will be moved to the folder that you will define in property moveToFolder
You can save this following code in Script Editor.app directly into your /Users/Your_User_Name/Library/Workflows/Applications/Folder Actions folder, as a .scpt file. Once the script is saved into that folder, it will be available to attach to any folder as a folder action using the Folder Actions Setup.
-- Set This To The Folder You Want Your Videos Moved To
property moveToFolder : (path to desktop as text) & "moved_videos"
-- Set This To The Kind Of File To Act Upon
property fileKind : "QuickTime movie"
on adding folder items to theFolder after receiving theNewItems
tell application "Finder"
set movieFilesRef to a reference to (files of entire contents of folder theFolder ¬
whose kind is fileKind)
move movieFilesRef to alias moveToFolder
end tell
end adding folder items to
OR Here Is A Version Which Defines The Name Extensions Of The Files To Act Upon
-- Set This To The Folder You Want Your Videos Moved To
property moveToFolder : (path to desktop as text) & "moved_videos"
-- Set This To The Name Extensions Of Files To Act Upon
property videoFileExtensions : {"m4v", "mkv", "avi", "mp4", "mov", "wmv", "mpg"}
on adding folder items to theFolder after receiving theNewItems
tell application "Finder"
set movieFilesRef to a reference to (files of entire contents of folder theFolder ¬
whose name extension is in videoFileExtensions)
move movieFilesRef to alias moveToFolder
end tell
end adding folder items to
When trying to duplicate files to another folder, the script throws a ~10006 error. This only occurs on some Mac mini computers and works fine on others. I have no idea why it is working on some computers but not on others.
This is the error shown:
Can't set "Macintosh HD:Users:username:Documents:" to <> "Macintosh HD:Users:username:Downloads:new test:portal resources" of application "Finder". (~10006)
tell application "Finder"
set folderToBeMoved to (container of (path to me) as text) &
"portal_resources"
set destinationFolder to path to documents folder as text
set moveFolder to duplicate folder folderToBeMoved to destinationFolder with replacing
end tell
expected output is duplicating file to documents folder. But, when testing on certain Macs, the script shows an error ~10006. It works on other macs perfectly well.
You are going to copy the folder to a literal string (path) which can fail on machines running older system versions.
Remove the as text parameter to get an alias specifier
set destinationFolder to path to documents folder
Try this code:
tell application "Finder"
set folderToBeMoved to folder "portal_resources" of container of (path to me)
set destinationFolder to path to documents folder
set moveFolder to duplicate folderToBeMoved to destinationFolder with replacing
end tell
There were two issues here that I've changed. First, you convert things to text strings and try to modify the strings, but the Finder has a rich language for talking about file objects. You should just leave everything in object form. For instance, this:
folder "portal_resources" of container of (path to me)
tells the finder to find the folder of that name in that container and return an object specifier that you can use directly.
Second, once you have this object, you can't add the 'folder' specifier to it. Where you say:
duplicate folder folderToBeMoved
folderToBeMoved is already an object specifier (an object of the form 'folder [path]') so you're actually asking the Finder for 'folder folder [path],' which throws the error you're seeing. It's like saying to someone "pass the 'pass the salt.'" People are probably smart enough to figure that out; the Finder isn't.
I actually have two questions.
How to exclude hidden files like .DS_STORE, Icon when I try to get files in folder ?
I've tried "without invisibles" but it seems not working.
How to set my var the_new_folder as an existing folder if already exists ?
Thanks for answers.
My code:
--
-- Get all files in a selected folder
-- For each file, create a folder with the same name and put the file in
--
tell application "Finder"
set the_path to choose folder with prompt "Choose your folder..."
my file_to_folder(the_path)
end tell
on file_to_folder(the_folder)
tell application "Finder"
-- HELP NEEDED HERE
-- HOW TO EXCLUDE HIDDEN FILES (Like Icon, .DS_STORE, etc)
set the_files to files of the_folder
repeat with the_file in the_files
-- Exclude folder in selection
if kind of the_file is not "Folder" then
set the_path to container of the_file
set the_file_ext to name extension of the_file
-- Remove extension of the file name
set the_file_name to name of the_file as string
set the_file_name to text 1 thru ((offset of the_file_ext in (the_file_name)) - 2) of the_file_name
-- Make the new folder with the file name
try
set the_new_folder to make new folder at the_path with properties {name:the_file_name}
on error
-- HELP NEEDED HERE
-- HOW TO SET the_new_folder AS THE EXISTING FOLDER
end try
-- Move the file in the new folder
move the_file to the_new_folder
end if
end repeat
end tell
end file_to_folder
tell application "Finder"
(display dialog ("It's done!") buttons {"Perfect!"})
end tell
Using the System Events context instead of Finder:
bypasses the problem with the AppleShowAllFiles preference[1]
is much faster in general.
Using the visible property of file / folder objects in the System Events context allows you to predictably determine either all items, including hidden ones (by default), or only the visible ones (with whose visible is true):
# Sample input path.
set the_path to POSIX path of (path to home folder)
tell application "System Events"
set allVisibleFiles to files of folder the_path whose visible is true
end tell
Simply omit whose visible is true to include hidden files too.
The code for either referencing a preexisting folder or creating it on demand is essentially the same as in the Finder context:
# Sample input path.
set the_path to POSIX path of (path to home folder)
# Sample subfolder name
set the_subfolder_name to "subfolder"
tell application "System Events"
if folder (the_path & the_subfolder_name) exists then
set subfolder to folder (the_path & the_subfolder_name)
else
set subfolder to make new folder at folder the_path ¬
with properties {name: the_subfolder_name}
end if
end tell
[1] In order to predictably exclude hidden items, a Finder-based solution is not only cumbersome but has massive side effects:
You need to determine the current state of the the AppleShowAllFiles preference (defaults read com.apple.Finder AppleShowAllFiles),
then turn it off.
then kill Finder to make the change take effect (it restarts automatically) - this will be visually disruptive
then, after your code has run, restore it to its previous value.
then kill Finder again so that the restored value takes effect again.
I believe that your first question is not a problem. Your code works fine for me.
As for the second issue, the simplest method is to use the text representation of the_path, and simply build the new folder, and see if it already exists:
set the_path_Text to (the_path as text)
set try_Path to the_path_Text & the_file_name
if (folder try_Path) exists then
set the_new_folder to (folder try_Path)
else
set the_new_folder to make new folder at the_path with properties {name:the_file_name}
end if
If you are truly having difficulty with the first code section, please post a new question with more details, such as a copy of the Result section of the Script.
Thank you to all of you ! #mklement0 #craig-smith
You will find below the corrected code with your help!
If I share this code, you want to be cited for thanks?
--
-- Get all files in a selected folder
-- For each file, create a folder with the same name and put the file in
--
-- Get user folder
set the_path to choose folder with prompt "Choose your folder..."
my file_to_folder(the_path)
on file_to_folder(the_folder)
tell application "System Events"
-- Get all files without hidden
set the_files to files of the_folder whose visible is true
repeat with the_file in the_files
-- Remove extension of the file name
set the_file_ext to name extension of the_file
set the_file_name to name of the_file as string
set the_file_name to text 1 thru ((offset of the_file_ext in (the_file_name)) - 2) of the_file_name
-- Make a new folder or get the existing
set the_path to POSIX path of the_folder
if folder (the_path & the_file_name) exists then
set the_new_folder to folder (the_path & the_file_name)
else
set the_new_folder to make new folder at folder the_path with properties {name:the_file_name}
end if
-- Move the file to the new folder
move the_file to the_new_folder
end repeat
end tell
end file_to_folder
-- Ending dialog
display dialog ("It's done!") buttons {"Perfect!"}
This is my first attempt at AppleScripting.
I have 3 folders that contain image files.
Test Folder 1 has 77 large, master files.
Test Folder 2 has 4 smaller files in a subfolder called ABC with the same name as files in Test Folder 1.
Test Folder 3 Contains an empty sub folder called ABC.
I want a script to check the file names in Test Folder 2 and copy the equivalent file names from Test Folder 1 to Test Folder 3 subfolder ABC
Here is what I have so far:
tell application "Finder"
set the_files to (files of folder "Macintosh HD:Users:Ronnie:Pictures:Test Folder 1:")
set the_file_names to (files of folder "Macintosh HD:Users:Ronnie:Pictures:Test Folder 2:ABC:")
set target_folder to ("Macintosh HD:Users:Ronnie:Pictures:Test Folder 3:ABC:")
if document files of the_file_names is equal to document files of the_files then duplicate the_files to target_folder
end tell
Currently it will copy all the files from Test Folder 1 so its seams that the "If" script does not work.
Can someone help?
Ronnie
This is one way to do it.
--this is my preference. I prefer to work with strings then coerce them to aliases
--"aliases" in the old AppleScript/Mac API sense, meaning special file/folder path data type
-- (this is what is returned when you use the choose file or choose folder commands, e.g.)
-- aliases can be coerced to and from strings
-- not to be confused with POSIX style paths which use "/" as separator, OR
-- Finder item objects, which are specific to the Finder ('file x of folder y of startup disk z' style)
set tf1 to "Macintosh HD:Users:Ronnie:Pictures:Test Folder 1:"
set tf2 to "Macintosh HD:Users:Ronnie:Pictures:Test Folder 2:ABC:"
set target_folder to "Macintosh HD:Users:Ronnie:Pictures:Test Folder 3:ABC:"
--above not needed in Finder tell block
tell application "Finder"
set fileNames to (name of files of alias tf1) --we get the names of the files here; "name" filters, so does "files"
set matchNames to (name of files of alias tf2) --and the names of the files we want to match in testing
repeat with thisName in fileNames --repeat loop is necessary for this
if thisName is in matchNames then
--notice the concatenating of folder and name to make new alias that gets copied
duplicate alias (tf1 & thisName) to alias target_folder
end if
end repeat
end tell