copy a file with applescript results in "Access Not Allowed" - applescript

I am trying to copy a file over another file using applescript.
tell application "Finder"
copy file "Macintosh HD:Users:rkohr:Dropbox:Hacks:hosts.block" to "Macintosh HD:Users:rkohr:Dropbox:Hacks:hosts"
end tell
Both files have be set with chmod 664, and should have permissions to be written to, but it gives a syntax error saying "Access Not Allowed"

First, 'copy' —as a command— doesn't mean 'copy a file' in applescript. The command to copy a file with the Finder is 'duplicate'. Second, what goes between the quotes is just a text string until it is prefaced with 'file' or 'alias' or some such keyword. So your second reference (i.e. the destination) needs to incorporate that. As is, you're trying to have the script copy a file to a string. Finally, renaming the resulting file is a separate action.
Try this:
tell application "Finder"
set sFil to file "rkohr:Dropbox:Hacks:hosts.block"
set fNam to name of sFil
set AppleScript's text item delimiters to ".block"
set sNam to first text item of fNam
set dFil to duplicate sFil to "rkohr:Dropbox:Hacks:" with exact copy
set name of dFil to sNam
end tell
NB If you have a variety of file extensions, then you can get the 'name extension' of your source file and use that as the delimiter. The 'exact copy' parameter will include the file's permissions in the copy. There is also the option of replacing an already existing destination file. I'm assuming that your file path is functional but having it reach back to the disk (e.g. prepend with "MacintoshHD:Users:" or whatever your setup is) might be worth considering.

Related

AppleScript - exclude certain files when duplicating a folder from one location to another

I have an AppleScript that is working to duplicate the contents of a source folder to a destination folder. I now need to add some conditional logic to this so that it excludes certain files from the source folder and doesn't copy across all files/folders.
Here's my current script:
set here to POSIX file "/Users/benny/Desktop/Projects/Source/Project1"
set there to POSIX file "/Users/benny/Documents/Masters"
tell application id "com.apple.Finder" to duplicate ¬
every item in the folder here to there
I would like to add some logic so that it doesn't copy across these files:
Import.log
Recover.log
Haven't tried to get the syntax working here but haven't been able to work out how to exclude files by their filename so far.
The duplicate command is wrapped in a try statement because it will error out if items of the same name already exist in there. You could uncomment the with replacing and get rid of the try statement, that is if replacing an existing item is okay.
set here to POSIX file "/Users/benny/Desktop/Projects/Source/Project1"
set there to POSIX file "/Users/benny/Documents/Masters"
tell application id "com.apple.Finder"
set theseItems to a reference to ¬
(items whose name is not equal to "Import.log" and ¬
name is not equal to "Recover.log") of folder here
try
duplicate theseItems to there -- with replacing
end try
end tell

Mac Automator - Compress (ZIP) a folder and rename as original folder name without the entirety of the path

Hi all, total newbie to any kind of programming, but I've written an Automator workflow to do a repetitive task I do daily. Basically I'm prepping artwork files and folders to send out to printers.
The steps are as follows:
Set Value of Variable (variable is ArtworkFolder)
where I drop the artwork folder containing the illustrator file and
PDF
Get Specified Finder Items
locates a file on a RAID server
Copy Finder Items (to ArtworkFolder)
copies said file into the folder
Ask for Confirmation -
prompts user to select an updated PDF
Ask for Finder Items -
opens the folder where the new PDF sits
Copy Finder Items (to ArtworkFolder)-
copies PDF file into the folder (overwriting older files)
Ask for Confirmation
prompts user to select print guidelines
Ask for Finder Items
opens the folder where the print guidelines sit
Copy Finder Items (to variable ArtworkFolder) -
copies print guidelines into the folder
THIS IS WHERE I THINK IT MAY BE GOING AWRY
Get Value of Variable (ArtworkFolder)
this is supposed to get the name of the original folder
Get Specified Finder Items
and all its contents
Create Archive
Then Zip the whole lot together
But it zips it with the name Archive.zip which I don't want so...
Rename Finder Items: Replace Text
which is set to find "Archive" in basename only ignoring Case. then
replace that with the variable ArtworkFolder
but it renames the folder with the entire path
I've tried to strip out the path by adding…
Rename Finder Items: Replace Text
which is set to find everything in the path basename only up to the
unique folder name and replace with nothing (eg I left the Replace box
blank)
But then it duplicates the folder name like this:
00000- Test Artwork Folder/00000- Test Artwork Folder.zip
All I want is to original folder name (00000- Test Artwork Folder) as
the zip file name.
Any tips would be greatly received!
For whatever reason, Automator doesn't have an included action to get file names, so you will need to use a third-party action or do it yourself. A Run AppleScript action can be used, but either way, note that you will need to save the workflow items and use the Ignore Input option as appropriate around getting the names, so you don't get them mixed in with the file items (also note that the workflow and action items are a list).
To get the name of a single item, something like the following can be inserted into your workflow, where the variable for the name can be used in the rename action:
Set Value of Variable { Variable: Original Input } -- stash current items
Run AppleScript: -- get the name
on run {input, parameters}
set {theContainer, theName, theExtension} to getNamePieces from (first item of input)
return theName
end run
to getNamePieces from somePath
tell application "System Events" to tell disk item (somePath as text)
set _container to path of container
set {_name, _extension} to {name, name extension}
end tell
if _extension is not "" then
set _name to text 1 thru -((count _extension) + 2) of _name -- just the name part
set _extension to "." & _extension
end if
return {_container, _name, _extension}
end getNamePieces
Set Value of Variable { Variable: Item Name }
Get Value of Variable { Variable: Original Input } (Ignore Input) -- continue workflow

How to fix a ~10006 error in applescript when duplicating a file to another folder?

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.

Moving entire folders with AppleScript

This is my very first use of AppleScript. I'm trying to use it to move a 100+ folders (and their content files) from one location to another. I'm not just moving the files because I want to maintain the filing system. So I've got the folders listed in a .txt file in this format:
Macintosh HD:Users:Tom:Music:iTunes:Afrika Bambaataa
Macintosh HD:Users:Tom:Music:iTunes:Air
And then I run the following in AppleScript:
tell application "Finder"
set TextFile to (choose file with prompt "Select your text file" of type {"txt"})
set My_Folder to (choose folder with prompt "Select your destination folder")
set List_files to paragraphs of (read TextFile)
move List_files to My_Folder
end tell
However the error I receive is the following:
error "Finder got an error: Handler can’t handle objects of this class." number -10010
Any help on what I'm doing wrong here?
Much appreciated!
tell application "Finder"
set TextFile to (choose file with prompt "Select your text file" of type {"txt"})
set My_Folder to (choose folder with prompt "Select your destination folder")
set List_files to paragraphs of (read TextFile)
move List_files to My_Folder
end tell
Currently, List_files is just a list of text objects, i.e.
{"Macintosh HD:Users:Tom:Music:iTunes:Afrika Bambaataa",
"Macintosh HD:Users:Tom:Music:iTunes:Air", ...}
so what you're asking Finder to do is move some text to a folder, which doesn't make sense. You need to tell Finder that this text represents a path to a folder, by using the folder specifier. Since this can't be done en masse, you have to iterate through the list:
repeat with fp in List_files
move folder fp to My_Folder
end repeat
However, I wouldn't actually do it like this, as that will require 100+ separate move commands - one for each item in List_files. Instead, we'll edit the list items first by prepending the folder specifier to each item, and then move the list of folders altogether in a single command:
repeat with fp in List_files
set fp's contents to folder fp
end repeat
move List_files to My_Folder
Depending how big the files are and how long the transfer will take, the script might time out before the transfer is complete. I'm not honestly sure what impact this would have on an existing file transfer. I suspect that AppleScript will simply lose its connection with Finder, but that the transfer will continue anyway since the command has already been issued (another benefit of using a single move command instead of multiples). But, if you want to avoid finding out, we can extend the timeout duration to be safe:
with timeout of 600 seconds -- ten minutes
move List_files to alias "Macintosh HD:Users:CK:Example:"
end timeout
The final script looks something like this:
tell application "Finder"
set TextFile to (choose file with prompt "Select your text file" of type {"txt"})
set My_Folder to (choose folder with prompt "Select your destination folder")
set List_files to paragraphs of (read TextFile as «class utf8»)
if the last item of the List_files = "" then set ¬
List_files to items 1 thru -2 of List_files
repeat with fp in List_files
set fp's contents to folder fp
end repeat
move List_files to My_Folder
end tell
The extra line that starts if the last item of... is just a safety precaution in case the last line of the text file is a blank line, which is often the case. This checks to see if List_files contains an empty string as its last item, and, if so, removes it; leaving it in would throw an error later in the script.
EDIT: Dealing with folders that don't exist
If the repeat loop throws an error due to an unrecognised folder name, then we can exclude that particular folder from the list. However, it's easier if we create a new list that contains only the folders that have been verified (i.e. the ones that didn't throw an error):
set verified_folders to {}
repeat with fp in List_files
try
set end of verified_folders to folder fp
end try
end repeat
move the verified_folders to My_folder
This also means we can delete the line beginning if the last item of..., as the check that this performs is now going to be caught be the try...end try error-catching block instead.

AppleScript: Garbage values written to file when using path to command

I am trying to write a simple applescript that organises the files in any selected folder. I want to make the script such that it runs at specific intervals and re-organizes the folder if something has changed. For this, I am trying to save the path of the user chosen folder to a file. Each time the script runs, it reads the folder path from this file.
here's a snippet from the code:
set home_path to get path to home folder
tell application "Finder"
set home_folder to folder (home_path as string)
if not (exists file "Clfd_config.cf1" in home_folder) then
set (folder_path) to choose folder with prompt "Choose the folder to organize"
set this_folder to folder (folder_path as string)
set path_file to open for access file (home_path & "Clfd_config.cf1" as text) with write permission
write folder_path to path_file
close access path_file
else
set path_file to open for access file (home_path & "Clfd_config.cf1" as string)
set folder_path to read path_file as string
set this_folder to folder (folder_path as string)
close access path_file
end if
end tell
However, when I open the file, it has garbled information, like so:
������Harshad��������������������œ‘xH+��� 7 Desktop����������������������������������������� ���������������� 7Éœ‘zç��������ˇˇˇˇ��I ���������� ������œ‘*∆������œ‘-5������D�e�s�k�t�o�p��� �H�a�r�s�h�a�d��Users/harshad/Desktop���/����ˇˇ������
When I try to read this file n the script, the script obviously fails.
I have tried telling the script to write the file as string, as text, but I keep getting the error that the folder_path variable cannot be converted to text or string.
What should I do so that the path is saved properly and the script can read it back from the saved file?
The main issue is that you're writing an alias file specifier to disk rather than a string path.
I added basic error handling while writing to disk and removed some redundant code
property configFileName : "Clfd_config.cf1"
tell application "Finder"
set configFile to (home as text) & configFileName
if not (exists file configFile) then
set folder_path to choose folder with prompt "Choose the folder to organize"
try
set fileReference to open for access file configFile with write permission
write (folder_path as text) to fileReference
close access fileReference
on error
try
close access file configFile
end try
end try
else
set folder_path to read file configFile
set this_folder to folder folder_path
end if
end tell
It seems you just want to save value of a folder path and be able to find it again during next run.
if only that, why are you using subroutine to write this path to a text file at specific place ? isn't it easier to you use the characteristics of "property" objects ?
"Property" variable can be changed and it keeps new value in the script itself, until next compilation.
Try this script bellow : the first run, it will ask you for folder. Select it. Any next run, it will just display the folder selected during first run !
property My_Folder : ""
if My_Folder is "" then -- first run, ask user to select folder
Set My_Folder to (choose folder with prompt "choose the folder to organise") as string
end if
display dialog "folder selected = " & My_Folder
it does the same thing than read/write from text file...

Resources