Applescript creating hidden directory and transferring files - applescript

I was wondering why this script of mine was incorrect. (Note: I am new to AppleScript, so please ignore how terrible it is. :P)
set public to "~/Public/"
set p to POSIX path of public
tell application "Finder"
make new folder at p with properties {name:".folder"}
end tell
set sfolder to POSIX path of ((path to me as text) & "::")
set tfolder to "~/Public/.folder/"
duplicate files of sfolder to tfolder
It says there is an error at
make new folder at p with properties {name:".folder"}
error "Finder got an error: AppleEvent handler failed." number -10000
What should I do?

First of all, AppleScript cannot expand a tilde.
Second of all, the Finder doesn't accept POSIX paths.
Third of all, the duplicatecommand must be in a Finder application tell block.
Fourth of all, while the Finder is able to create the invisible folder it cannot copy the files because invisible files are only considered if "Show Invisible Files" is set to true in the preference file which is not reliable.
I recommend to use the shell for the whole task, ditto can duplicate the files and create intermediate directories simultaneously.
POSIX path of (path to public folder) is the same as ~/Public but returns the full path.
I don't know what the two colons stand for, so I just omitted them.
set publicSubFolder to POSIX path of (path to public folder) & ".folder"
set myself to POSIX path of (path to me)
do shell script "/usr/bin/ditto " & quoted form of myself & space & quoted form of publicSubFolder

Related

Apple Script: Copy file (being selected from prompt) to another location

I am struggling with my Apple Script. I am asking to select a file and the selected file needs to be copied to another location. I am new to Apple Scripting, so probably made some mistake. I tried different versions with "copy" instead of "duplicate" or "alias" instead of "file", but nothing worked so far. Hope, somebody can help me figure this out.
This is what I scripted so far (I get an AppleEvent timed out):
set DefaultPath to POSIX file "/Users/jan/Library/Mobile Documents/com~apple~CloudDocs/FOLDER/Test"
set DestFolder to "/Users/jan/Library/Mobile Documents/com~apple~CloudDocs/FOLDER/Destination"
set theFile to (choose file with prompt "Select file:" default location (DefaultPath))
tell application "Finder"
duplicate theFile to folder DestFolder
end tell
The problem is that the Finder doesn't support POSIX paths.
I recommend to use a relative path path to library folder from user domain and HFS paths (colon separated)
To satisfy the location parameter of choose file put the alias keyword in front of the (HFS) path
set cloudDocs to (path to library folder from user domain as text) & "Mobile Documents:com~apple~CloudDocs:"
set DefaultPath to cloudDocs & "FOLDER:Test:"
set DestFolder to cloudDocs & "FOLDER:Destination:"
set theFile to (choose file with prompt "Select file:" default location (alias DefaultPath))
tell application "Finder"
duplicate theFile to folder DestFolder
end tell

AppleScript - Copy All files/folders from source folder to destination folder

I'm trying to write an AppleScript that will simply copy the contents (both folders and files) from a specified source folder to a specified destination folder. At the moment my script runs but only copies one file and I can't work out how to get it to copy all files in the folder.
Here's my script:
set sourceFolder to (POSIX file "/Users/benny/Desktop/Projects/Source/Project1") as alias
set destinationFolder to (POSIX file "/Users/benny/Documents/Masters/Project1") as alias
tell application "System Events"
set availableSourceFiles to every file of sourceFolder whose visible is true
set filesOfTargetFolder to files of destinationFolder whose visible is true
end tell
-- if no more source file is available, quit this script
if (count of availableSourceFiles) = 0 then
quit
end if
set sourceFile to (first item of availableSourceFiles) as alias
-- use the Finder to copy the file
tell application "Finder"
-- duplicate the file to the target folder
duplicate sourceFile to destinationFolder
end tell
I'm assuming I need to include a for each type loop but can't get the syntax correct here. Haven't written AppleScripts in many years so trying to remember how it all works.
If the destination "Project1" folder doesn't have stuff in it already, then duplicating the folder is likely to be quicker:
tell application id "com.apple.Finder" to duplicate folder POSIX file ¬
"/Users/benny/Desktop/Projects/Source/Project1" to the folder ¬
POSIX file "/Users/benny/Documents/Masters" with replacing
However, if that's not an option, then I'd stick with your method and copy the contents of the folder across instead:
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
Bear in mind that if there's a file or folder at any of the destinations that intended to be occupied by one of the incoming source items, Finder will throw an error. You would typically incorporate some sort of check ahead of the copy.

How to move a file to trash in AppleScript?

I know its a dumb question but somehow the command i type is not working
set deleteFile to srcTemp & "Archive.zip"
--deleteFile path is something like this /Users/home/Desktop/Archive.zip
tell application "Finder"
move POSIX file "" & deleteFile & "" to trash
--move file "\"" & destNoQuote & "Archive.zip\"" to trash
empty the trash
end tell
But I get an error saying can't find the POSIX file.
I know that many people use the posix file command inside the Finder tell block of code however that's a mistake. The posix file command is not a Finder command, it's an applescript command, and therefore should not be in the Finder block if possible. This is true for all commands actually. You should only tell an application to perform the commands you can find inside of its applescript dictionary otherwise you will see unexpected behavior... as you are finding.
As such this is how you should write your code...
set deleteFile to srcTemp & "Archive.zip"
set posixFile to POSIX file deleteFile
--deleteFile path is something like this /Users/home/Desktop/Archive.zip
tell application "Finder"
move posixFile to trash
empty the trash
end tell
Here's another method of your entire script in one line:
do shell script "rm -Rf " & quoted form of (srcTemp & "Archive.zip")
This will force remove your file and it doesn't just go to your trash, it's gone.

Copy mounted folders to local folder

We have a samba share from where I'd like to copy folders with an applescript. This is what I already have (the mounting works):
mount volume "smb://samba.com/e_18_data11$"
delay 3
set sourcefolder to ("smb://samba.com/e_18_data11$/e_18_data11$/folder1/folder2" as POSIX file)
set localfolder to ("/Users/username/Dropbox/Test" as POSIX file)
tell application "Finder" to duplicate sourcefolder to localfolder
This gives me still this error:
the routine can not edit objects of this class." number -10010
I tried and combined many solutions already on SO, e.g. this solution
– OS X 10.9
It's probably the sourcefolder specification that is wrong.
I think you can just use the volume name instead of "smb://".
set sourcefolder to ("/Volumes/7samba.com/e_18_data11$/e_18_data11$/folder1/folder2" as POSIX file)
(if the mounted volume is named "7samba.com")
Tip: drag the actual sourcefolder from Finder into your AppleScript. It should paste the path into the script. Use that path for sourcefolder.
More:
The Error your getting is:
Mac OS error -10010 (telBadHTypeErr): bad hook type specified
I tested it (with two local folders) to see if the script would work. It did work and duplicated the folder.
You can (or should anyway) wrap critical code into a try block, like this:
try
duplicate sourcefolder to localfolder
on error the error_message number the error_number
display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
end try
This way you can check and react to errors.
Addition:
May be you can check for existence like this:
tell application "Finder"
set aBoolean1 to get (exists sourcefolder)
set aBoolean2 to get (exists localfolder)
end tell
log aBoolean1
log aBoolean2
Both bool's must be YES

applescript Posix chaos

Yet again flummoxed by syntax and ordering of an applescript workflow solution
When I create the following code
tell application "Finder"
set remote_p to alias (POSIX file '/Volumes/WAM XSAN/Audio/AAA)
set main_folder to (make new folder at remote_p with properties {name:temp_name}) as alias
end tell
Everything works fine. However, I need to create the main_folder in different locations dependent on the input "client_code" and "department", So i tried this:
tell application "Finder"
set x_san to "/Volumes/WAM XSAN/"
set x_sannewpath to (x_san & department & "/" & client_code)
set x_sanfolder to POSIX file x_sannewpath
set remote_p to alias (POSIX file x_sanfolder)
set main_folder to (make new folder at remote_p with properties {name:temp_name}) as alias
end tell
And the error comes back "Can't get "Volumes/WAM XSAN/Audio/AAA" of Application Finder"
Where am i going wrong in setting up the POSIX paths?
Please help!!!!
Try this example. I tried to model it after what you're trying to do. Note that I have moved most of the code outside the Finder tell block of code. The Finder does not know the "posix file" command. That's an applescript command, not a Finder command. Also you don't need the Finder to set variables and add strings together. We can do all of that outside the Finder.
Even though it sometimes works when you have the "posix file" command inside a Finder tell block of code, it's always best if you only tell applications to do what they know how to do. You can find what an application knows how to do by looking in its applescript dictionary. Check the Finder dictionary and you will see that "posix file" is not one of its commands.
Anyway, this will create a folder on your desktop. I hope this helps.
set x_san to "/Users/hmcshane/"
set client_code to "Desktop"
set temp_name to "test folder"
set x_sannewpath to x_san & client_code
set applescript_x_sannewpath to POSIX file x_sannewpath
tell application "Finder"
set main_folder to (make new folder at applescript_x_sannewpath with properties {name:temp_name}) as alias
end tell
Try:
set department to "Audio"
set client_code to "AAA"
set temp_name to "New Folder"
set x_san to "Volumes/WAM XSAN/"
set x_sannewpath to x_san & department & "/" & client_code
tell application "Finder" to set main_folder to (make new folder at POSIX file x_sannewpath with properties {name:temp_name})

Resources