How to detect and manipulate files on a USB using AppleScript - applescript

I have recently started a little project of mine using languages such as Python or AppleScript to manipulate files and other things on a Mac. I am making multiple different programs that do these things.
Recently I began to write a program that changes the desktop background to a file on the USB stick that the program is located on. It is intended to work across different computers, so I have to keep it on the USB stick.
The issue is that when I run the program from my computer, the AppleScript seems to not be able to find the files on the USB. I am also not sure whether or not I will need to change the code when I move the program onto the USB.
The program works fine when I use files on the computer itself, but it seems to come up with an error every time it tries to retrieve an image from the USB.
Here is the appropriated code.
set Username to system attribute "USER"
set Photo to (random number from 1 to 5) as text
tell application "Finder"
make new folder at desktop with properties {name:"DesktopFolder"}
move entire contents of folder "USB/Photos/" to folder "Users/" & Username & "/Desktop/DesktopFolder/"
set desktop picture to POSIX file "/Users/" & Username & "/Desktop/DesktopFolder/Image" & Photo & ".png"
end tell
Of this, the line move entire contents of folder "USB/Photos/" to folder "Users/" & Username & "/Desktop/DesktopFolder/" is the one producing the following error Finder got an error: Can’t get folder "USB/Photos/".
What I would like to happen is that the images from the USB are copied into the new folder on the desktop, and then a random photo from this folder is chosen as the new desktop background.
Any help would be appreciated. Thanks!

The main issue is that Finder doesn't understand posix paths (i.e. slash notation); it operates using HFS paths, which uses colons in place of slashes, and substitutes in the disk name at the beginning of the file path.
So, the equivalent HFS path that points to the same location as the posix path
/Users/{{user}}/Desktop/DesktopFolder/Image4.png
is written as
Macintosh HD:{{user}}:Desktop:DesktopFolder:Image4.png
Therefore, to point to a folder on your USB drive named "USB", you would write:
USB:Photos:
Here's a modified and corrected/improved edit of your script:
set n to (random number from 1 to 5) as text
tell application "Finder"
set F to make new folder at desktop with properties {name:"DesktopFolder"}
move every item of folder "USB:Photos:" to F
set desktop picture to the file ("Image" & n & ".png") in F
end tell
I've done away with a lot of your path references that were expressed explicitly as broken string fragments, and instead set a variable to store a reference to the location of the folder you make on your desktop. That way, you can use the variable reference to target that folder for moving and selecting files.

Related

How to duplicate desktop file on applescript

I'm trying to duplicate my desktop file on apple script using script editor but I can't figure out how to do it. Can someone please help me?
The following command will duplicate your Desktop folder in Finder as Desktop copy and will be in your Home folder:
tell application "Finder" to duplicate desktop
The following commands will duplicate the target object directly on the Desktop, i.e., objects within the Desktop folder in your Home folder in Finder .
The following command will duplicate every file on your Desktop:
tell application "Finder" to duplicate every file of desktop
The following command will duplicate every folder on your Desktop:
tell application "Finder" to duplicate every folder of (path to desktop)
The following command will duplicate every file and folder on your Desktop:
tell application "Finder" to duplicate every item of (path to desktop)
NOTES:
This is to address the comment made by has:
As to: "1. You don’t need path to desktop as Finder’s application object already has a desktop property containing a reference to the user’s Desktop."
This is not categorically a true statement and depends on how it's being used. It's true with the Desktop folder itself and files within the Desktop folder, however it is absolutely false with duplicate every folder of and duplicate every item of, and without using (path to desktop) in these instances it errors out with the following error statement:
"Finder got an error: The folder “” can’t be moved into itself." number -122
As to: "2. Never do duplicate every item of desktop – if the Finder’s preferences are set to display mounted volumes on the Desktop then those will be included too, and you definitely don’t want to duplicate those!"
That statement is categorically false!
He/she (has) obviously doesn't know what he/she is talking about because mounted volumes are not located within the Desktop folder in your Home folder. By default, they are mounted at /Volumes and while they can appear on the Desktop, they are not directly connected to the Desktop folder in your Home folder.
In this reference the Desktop and Desktop folder in your Home folder are not the same thing!

Login item stored on SMB share causes Warning when not connected to the network

I wrote an Apple Script that mounts all of our company network shares and saved it as a .app to a general network folder that all users can read. I then added the file to the login items on my test mac. When I am connected to the network, the script runs fine.
The problem is, when the computer is off the network (simulating the user bringing the laptop home), after you log in, the system obviously cannot find the file any more and prompts the user "there was a problem connecting to ...".
I know the obvious answer is to store it locally on the system but I want to be able to manage this file and make changes as needed.
Is there a way to suppress this warning?
I made a bit more tests, and here is script and method for you :
1) mount your share drive, and , in Terminal, type "ls " (don't forget space) and drop your file.app. you do not need to hit return, this is just to get right path. you should see something like "/Volumes/drive_name/Path_folder/File.app" (Drive_name and Path_Folder values will depend of your share drive structure).
2) remove the "/Volumes/" part at beginning of that path. change the "/" by ":" and store the new "finder path" in the first line of script bellow : (take care is you have spaces, because Unix has replaced them by "\ ". please also replaces them back to standard spaces.
make the script bellow (the 1 line contains your new path)
set Finder_Path to "drive_name/Path_folder/File.app"
tell application "Finder" to set OnLine to (Finder_Path exists)
if OnLine then
try
do shell script "osascript " & (POSIX path of Finder_Path)
end try
else
display dialog "sorry, you're off line"
end if
This script checks first that Finder can access to the file (= the drive in mount !). if it can, then it runs the file.app. if no, then it displays message.
I tested ok replacing file.app by application made with Applescript.

AppleScript: open frontmost file with another application

I'd like to write an AppleScript program to do the following (Automator would be fine too):
I want to open the current active TextMate file (possibly there are several tabs open and other windows) with the application Transmit 2. (This will upload the file over FTP using Transmit's DockSend feature.)
Here I've used a specific application (TextMate) but ideally I'd like it to work for any file currently active in any application.
Ultimately I will assign a keyboard shortcut to run it.
Here's what I have so far:
tell application (path to frontmost application as text)
set p to path of document 1
end tell
tell application "Finder"
open POSIX file p using "Transmit 2"
end tell
I've tried many variants of this and nothing works.
EDIT:
I have found this page: http://wiki.macromates.com/Main/Howtos and someone has made exactly the script I'm looking for:
tell application "Transmit" to open POSIX file "$TM_FILEPATH"
This is for Transmit [not 2] and I think for TextMate pre v2. I get the error (when using Transmit 2):
Transmit 2 got an error: AppleEvent handler failed.
One of the updates to v2 has broken it (not sure which one).
There appear to be two steps to your problem. One, get the path to the document (or some other reference that allows you to later open the document), and, two, open the document in the desired application.
If the AppleScript is saved as an application, the frontmost application is the AppleScript you’re running, and so that path will be the path to the AppleScript application. In that case, I’m not aware of how to get the second-frontmost application.
However, if the application supports the scripts folder (go into AppleScript Editor‘s preferences, and enable “Show Script menu in menu bar”), you can save the script as a “Script“ in the User Scripts folder, and when run from the scripts menu the frontmost application will be the application you’re currently in.
You may want to display the p variable when testing to ensure that you are getting the correct path and not the path to the AppleScript.
As far as opening the document in another application (such as Transmit), the best way to do this is to talk to the application directly if it supports it:
tell application (path to frontmost application as text)
set p to path of document 1
end tell
--for testing: verify that the path is for the correct document
display dialog p
tell application "Transmit 2"
open p
end tell
I don’t have Transmit, but I’ve verified that this works if I replace “Transmit 2” with Textastic or Smultron 6.
If you do need to use the Finder to open the document, the Finder seems to prefer its paths as strings, and also seems to prefer a full path to the application. Something like this should work:
tell application (path to frontmost application as text)
set p to path of document 1
end tell
--for testing: verify that the path is for the correct document
--display dialog p
set transmitPath to path to application "Transmit 2"
set p to POSIX file p as string
tell application "Finder"
open file p using transmitPath
end tell
Again, I’ve tested this using Textastic and Smultron as the applications.
The most common solution for the problem you are trying to solve is to run an app that makes your Web server appear to be a mounted Mac disk. Transmit 4 has that feature, which Panic calls “Transmit Disk.” But there are a few other apps also — Transmit was not the first.
Your Mac apps (and AppleScripts) just see a typical Mac disk that they can save files to and read files from (the most basic of basic AppleScript tasks) and yet Transmit Disk (or similar app) is transparently mirroring any changes to that Mac disk to your Web server in the background. This makes all the network and FTP stuff totally go away and leaves you writing very simple scripts that do very powerful things to your Web server. You Save HTML documents on there, you Export image and movie files onto there as easily as you would Save them on your Desktop, and yet they are immediately published to your Web server. Even the only barely scriptable apps can Save their files onto a particular disk.
For example, if I have an HTML document open in BBEdit and I want to save a copy of that document to my Web server, it only takes a few lines of code, which would likely be similar in any AppleScript-able text editor (this script would also work verbatim in the free TextWrangler):
tell application "BBEdit"
set theHTMLSource to the contents of text window 1
make new document with properties {text:theHTMLSource}
save document 1 to file "Transmit Disk:index.html"
close document 1 saving no
end tell
Notice that the AppleScript above not only doesn’t have to know anything about SFTP or have any login credentials, it doesn’t even have to figure out the file path of my current document because it just takes the content right out of the current window. There are no POSIX pathnames, no shell scripts, no monkey business at all. And because this task and code is so simple, you could conceivably rewrite this script 20 times for 20 different apps that you may use, so that they can all Save a copy of their current document onto your Transmit Disk, and thus publish that document to your Web server.
And if I have a folder of images that goes along with that HTML document, I can ask Finder to duplicate that folder onto my Transmit Disk to publish it. With just one line of code:
tell application "Finder"
duplicate folder "images" of (the path to the desktop folder as alias) to the disk "Transmit Disk" replacing no
end tell
… but those images could also be exported out of Photoshop or any app, right onto the Transmit Disk, via AppleScript.
In short, the thing that all of your Mac apps have in common is they can all Save files to a Mac disk. They can’t necessarily all give you the pathnames to the documents they have open, or open those files with Transmit. And Mac apps and AppleScript were designed primarily to work with files Saved or Opened to/from local disks. So you gain a lot if you use something like Transmit Disk to make your Web server basically part of the AppleScript party, by making it appear to be just a plain old Mac disk.

moving one folder to another folder - applescript

I'm trying to move a user-selected folder to another folder, but I don't get it: whenever I look it up, it looks too complicated, even though I know it's not supposed to be.
Here's the code I have so far - please help:
choose folder with prompt "Select folder:"
on open of finderObject
tell application "Finder" to move (finderObject) to folder "Library:Application Support"
end open
end
First off, you have to assign the chosen folder to a variable.
Also, many special folders in the OSX file structure have special names to get their paths, including the library and the application support folders. So, your script can simply be:
set finderObject to choose folder with prompt "Select folder:"
tell app "Finder" to move finderObject to folder (path to Application Support folder from local domain as string)
However, that is the root level Library folder. I suspect you will want to use the Application Support folder in the ~/ user domain. For that, change the "from local domain" to "from user domain".

Move folder to external drive via applescript

I need to MOVE (not copy) every file (including subfolders) in "Downloads" to an external drive called "Drive" in the location "Volumes/Drive/Apple/MacBackup/Downloads". I have tried countless times and it is just not working... Here is my code right now:
tell application "Finder" to move entire contents of folder "Users:myUsername:Downloads" to folder "Drive:Apple:MacBackup:Downloads"
I am getting the error:
Finder got an error: Can’t get folder "Users:myUsername:Downloads".
HFS paths have to begin with the name of the disk, like
"Macintosh HD:Users:myUsername:Downloads"
You have probably taken it from a POSIX path, were the first slash represents the startup volume.
To be sure just run
choose folder
and copy the path from the result.
The error is because that is not a correctly formed path. You should use the special folder designation (path to downloads folder).
tell application "Finder" to move entire contents of (path to downloads folder) to folder "Drive:Apple:MacBackup:Downloads"
Also, I would strongly encourage you to copy the files, then delete the old files. Moving files is asking for trouble if something happens mid-process, and it's lost in transit. (think star trek transporter problem)

Resources