AppleScript: Error -15267 Copying to External - applescript

I recently started AppleScript and wanted to slowly getting into scripting and hoping someone can help me with a block I have ran into. Tried a few things and moving pieces around and changing identifiers but don't know what I should be looking for to change. Hoping someone can help me out and explain my flaws.
Background Story: Trying to test a script to back up of a user account home folder to an external drive.
What I have first: drive verification to make sure there is a drive plugged into the computer. Followed by setting source and destination. Then tried compiling an if then statement to single out 2 folders I don't want to be copied over (only focusing on the user folder, but unsure if that is correct and will need that looked at too). I get my error at the duplicate command: "Finder got an error: An item with the same name already exist in this location." However, there is nothing the external so I have no clue what else to do. Any advice would be helpful.
This is the code I have to start with:
tell application "System Events" to set diskNames to name of every disk
if "Untitled" is in the diskNames then
display dialog "Disk is mounted" buttons {"OK"} default button 1 with icon 1
else
display dialog "No disk found" buttons {"OK"} default button 1 with icon 1
end if
tell application "Finder"
set source_folder to "Macintosh HD:Users:"
set tar_disk to "Untitled:"
if source_folder is not {"administrator", "Shared"} then
with timeout of 3600 seconds -- 2 hours
duplicate source_folder to tar_disk
end timeout
else
display dialog "No folder detected" buttons {"OK"} default button 1 with icon 1
end if
end tell

You are able to copy only your own home folder with the Finder, you don't have permissions to copy the home folders of other users (except Shared).
Basically it's good habit to add Finder specifiers to literal strings representing files, folders and disks for example
set tar_disk to disk "Untitled"
Since there is a home property pointing to the current home folder you can write
tell application "Finder"
set tar_disk to disk "Untitled"
with timeout of 3600 seconds -- 2 hours
duplicate home to tar_disk
end timeout
end tell
There is an important caveat: The Finder copies only the visible files and folders. If AppleShowAllFiles is set to false all files starting with a dot are ignored. To copy the entire home folder something like the shell rsync command is the better choice – also for speed reasons.

Related

How can I create a .app file to open DMG files with, to run AppleScript with that file as input?

What I ultimately want to do is have a .app that I can open DMG files with. When the DMG file is opened the app, it opens the DMG in a new Finder tab and then shows the Toolbar in Finder after half a second. AppleScript for the later would look like this:
delay 0.5
tell application "Finder"
tell the front window to set toolbar visible to true
end tell
I tried looking up how to open a DMG file in a new Finder Tab with AppleScript but didn't find a solution. If I end up not finding one, then I'll see if I can do so with a Terminal command and then do it in AS with do shell script. For the file input, I tested this code based off what I found here but it doesn't work, it just creates a copy of the DMG:
on run {input, parameters}
if input is {} then
set inputFile1 to ¬
quoted form of POSIX path of ¬
(choose file with prompt "Please select a file to process:")
else
set inputFile1 to quoted form of ¬
(POSIX path of first item of input)
end if
display dialog inputFile1
end run
I did a bunch of googling as well for how to do so but I found nothing. What I can do to end up with an AppleScript that does everything I described?
I would open the dmg with hdiutil which seems to open the image into a standard window:
set runDMG to quoted form of POSIX path of (choose file)
do shell script "/usr/bin/hdiutil attach " & runDMG
Basically, you get the posix path of your dmg file and feed it to hdiutil attach. The disk image will open and —for me at least— consistently opens in a default finder window (i.e. same size as other windows, list view, sidebar visible, toolbar visible).
I should add that typically when I double-click on a dmg, it opens in a 'drag to applications folder' window which is icon view and shows only the app and the applications folder.
Separately, this code seems to work with bringing the disk image's folder to the front window.
tell application "Finder"
parent of startup disk
--> computer container of application "Finder"
open computer container
set target of window 1 to (last item of window 1 as alias)
--> disk "Key Codes" of application "Finder"
end tell
There are (at least on my Sierra mac), three items in the 'computer' folder: 'Network', 'Remote Disc', the startup volume. When you open a dmg, the virtual appears here as a 'removable'. I've found that when you get a list of folders in the computer container, the removable is the last item in the list. I don't know if that ever changes, and I haven't played around with multiple disk images simultaneously but it provides a starting point for specifying the desired window if for some reason, it doesn't open automatically.

Finding file type without using info for command? (macOS-AppleScript)

I am trying to walk through several servers with thousands of files each and log some information about them.
I had it set to ignore alias, or so I thought. If I use the "info for" command to identify the file and it's a broken alias it will pop up an "There was a problem connecting to the server 'X.X.X.X'" message. This wouldn't be too much of a problem, except it doesn't seem to recognize this as an ACTUAL error. It just sits there with the window open waiting for me to click "OK" before it will move on.
Anyone have an ideas?
Most of the properties of info for are also available in System Events.
If you've got a string path Macintosh HD:Users:MyUser:Desktop:something.ext named thePath then both
set fileType to file type of (info for alias thePath)
and
tell application "System Events"
set fileType to file type of disk item thePath
end tell
return the same information.

Make apple script code work on any laptop (not need the user's name)

I have a simple code of deleting a file and making a folder. But how do I make it run on any laptop because everyone's user name is different. I know it has something to do with "~" but i'm not sure where.
The code below works properly but how do I make sure it can run on any mac:
try
tell application "Finder"
delete ((POSIX file "/Users/myname/Desktop/thefile.txt"))
make new folder at (POSIX file ("/Users/myname/Desktop")) with properties {name:"thefolder"}
end tell
on error
display dialog ("That didnt work") buttons {":("}
end try
The syntax independent of the user name is much simpler since the desktop folder of the current user is the root folder of the Finder
try
tell application "Finder"
delete file "thefile.txt"
make new folder at desktop with properties {name:"thefolder"}
end tell
on error
display dialog ("That didnt work") buttons {":("}
end try
You can also
set username to ""
tell application "System Events"
set username to full name of current user
end tell
try
tell application "Finder"
delete ((POSIX file "/Users/username/Desktop/thefile.txt"))
make new folder at (POSIX file ("/Users/username/Desktop")) with properties {name:"thefolder"}
end tell
on error
display dialog ("That didnt work") buttons {":("}
end try

How do I Copy files to a USB with apple script that runs on said USB?

I run a computer lab at Denver University. We are having an issue with our backup system, and I need to copy/duplicate all of the files on the (Apple) computers onto a USB that I keep at my desk.
At the moment, I need to plug in a USB into each of the computers and manually duplicate each of the items of the "documents" folder and deposit them into a new folder on my USB. I need an Apple Script that I put on my USB, insert my USB into a computer, activate the app, and it will make a new directory named "(User name) upload", and deposit all of the items into that directory. Here is what I have so far:
tell application "Finder"
set theFolder to disk / Volumes / Lexar / stuff
set Files1 to Users / matanya / documents
tell application "Finder"
try
duplicate file Files1 to theFolder
on error
tell application "Finder"
display dialog "Transfer Failed"
end tell
end try
end tell
one of the issues is that every time I run the script, I get an error that says the the variable "Volumes" is not defined. Another one is that I am afraid that when I plug this script into another computer, it will not find the folder "matanya" that I have in my directory. Is there a way to call it "home" or something?
First of all the Finder accepts only HFS paths – starting with a disk name and colon separated. The folder Volumes doesn't matter when using HFS paths.
path to documents folder points always to the folder Documents of the current user.
set theFolder to "Lexar:stuff:"
set documentsFolder to path to documents folder
tell application "Finder"
if not (exists disk "Lexar") then
display dialog "Insert disk 'Lexar'" buttons "Cancel" default button 1
end if
try
duplicate documentsFolder to folder theFolder
on error
display dialog "Transfer Failed"
end try
end tell
More sophisticated this version creates a folder with the short user name of the current user and duplicates the documents folder to that individual folder
set theFolder to "Lexar:stuff:"
set currentUser to short user name of (system info)
set documentsFolder to path to documents folder
tell application "Finder"
if not (exists disk "Lexar") then
display dialog "Insert disk 'Lexar'" buttons "Cancel" default button 1
end if
if not (exists folder currentUser of folder theFolder) then
make new folder at folder theFolder with properties {name:currentUser}
end if
try
duplicate documentsFolder to folder currentUser of folder theFolder
on error
display dialog "Transfer Failed"
end try
end tell
With launchd you even could run this script automatically whenever the USB drive is inserted.

How to run script upon opening folder?

I want to run an applescript when a specific folder of mine is opened. Then, depending on the input, close the folder, or leave it open. All that without opening the folder. So, basically:
1) Try to open folder
2) Folder doesn't open, but window pops up
3-A) If user clicks ok folder opens
3-B) If user clicks cancel, it just exits out of script, leaving the folder unopened.
How can I do this? Remember: The folder CAN'T open in the background, it can ONLY open if the user presses OK. Help?
For this task, you'll want to use Folder Actions, which trigger script files. Below are the events involving a folder that could trigger a script, and the special corresponding handlers.
A folder is opened — on opening folder window for this_folder
A folder is closed — on closing folder window for this_folder
One or more items are added to a folder on adding folder items to this_folder after receiving these_items
One or more items are removed from a folder — on removing folder items from this_folder after losing these_itmes
A folder window is moved — on moving folder window for this_folder
The first bullet is what you'll be focusing on in this case. As the bullet implies, the script is triggered only when the folder actually opens. However, you can hack your way around this issue simply by adding this line at the very beginning of your script:
tell application "Finder" to close first window whose target is this_folder
And here is the full script:
on opening folder window for this_folder
tell application "Finder"
close first window whose target is this_folder
-- the window may appear briefly, but at least you've accomplished your goal
display dialog "Open folder " quoted form of the name of this_folder & "?" buttons{"Yes,"No"} default button 1 cancel button 2
-- pressing the "No" button is exactly the same as pressing the "Cancel" button on a regular dialog, and the script terminates
make new Finder window with properties {target:this_folder}
end tell
end opening folder window
SAVE YOUR SCRIPT FILE IN THE FOLDER ACTIONS FOLDER OF YOUR LOCAL SCRIPTS FOLDER. Create the folder yourself if it doesn't already exist. Doing so will allow your script to even run. Now, for this to function properly, you will need to attach the saved script file—not an application/application bundle—to your desired folder. To do this...
Perform a right-click on your desired folder
Click the menu item "Folder Actions Setup" at the bottom
Locate your saved script in the dialog that appears
Click "Attach"
Close the "Folder Actions Setup" window
Once you've done this, your script is ready to go.
Addendum: If your folder contains confidential information, beware that, as stated by user57368, "it is probably not possible to make the system even remotely secure with AppleScript."
Instead of an ordinary folder, you want an application bundle containing a folder or disk image. If you use an encrypted disk image, it will be more difficult for the user to access the contents without following your preferred procedure, but beware that it is probably not possible to make the system even remotely secure when you're using AppleScript.
If you just want to present a license agreement or other static notice before allowing the user to read the data, just read the hdiutil man page for how to create a disk image that presents a license agreement upon mounting.

Resources