This is going to sound like a dumb question because I can't figure out how to word it properly. But basically I want to create a package that contains a photo and then a script to set that photo as the wallpaper for that device. This is specifically for Catalina so I am not able to modify the Desktop Photos folder. The issue that I am running into is that if I download the photo the file path is going to be attached to the user. But I can't use something like $currentuser as part of of the file path. Just hoping for any ideas at all on how to get around this. I am very new to applescript as well so probably something that I am missing. Thank you.
tell current desktop
set picture to file "/Users/lemur/Pictures/bluemoon.jpg" as POSIX file
end tell
end tell
Here are five two-line examples which could be helpful. Run each and inspect the result:
set fd to path to desktop
return fd
set fd to path to applications folder
return fd
set fd to (path to applications folder from user domain)
return fd
set fd to path to pictures folder
return fd
set fd to path to desktop pictures folder
return fd
As you are talking to System Events anyway the syntax is pretty simple
tell application "System Events"
tell current desktop
set picture to file "bluemoon.jpg" of pictures folder
end tell
end tell
pictures folder is always the pictures folder of the current user
Related
I am trying to automate label generation from MS Excel into Photoshop using AppleScript. I can get all the data from Excel just fine, however getting that data into a predefined Photoshop document causes problems.
Specifically, I am unable to get Photoshop to open the template file which is located in the same folder as the AppleScript itself. I have tried numerous ways, including using a tell finderblock like this
tell application "Finder"
set myFolder to container of (path to me) as text
end tell
set template_path to myFolder & "CC.psd"
tell application "Adobe Photoshop"
open template_path
end tell
What sort of magic do I need to perform?
PS: Currently this is running on my local drive, however it would be awesome if it would work on network volumes as well.
Thank you
Tobias Timpe
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.
I'm trying to figure out how to open multiple finder windows at the same time; like opening several at a time.
Arbitrary example:
tell application "Finder" to open (every folder of home whose name starts with "Do")
See also How do I ask a good question?, and show your existing code.
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.
So I have this idea for a handy little AppleScript which in my opinion would be very handy in speeding up the process of uploading a local file, to its same location on the server.
In other words, you have to specify the home folder on the server and locally, but once that's finished, it would be nice to just press like "Command" + "Shift" "U" for upload or some other hot key combination not in use by OS X for uploading the currently selected file in the Finder.
I find myself needing to do this a lot, and it will save a lot of time!
Someone please tell me if there is an easier way to do this, but I think this will be a good learning experience on top of it all.
I need some help on how I should get started however..
1) the command line program curl can upload files. 2) if you have a file selected in a Finder window applescript can get the selection. Using those 2 ideas you can automate your task. I don't know the exact curl command but that should be easy to find using google. So you select a file in the Finder and then run the script. The script can be run with a keyboard shortcut as you mentioned or just put it in the Script menu and run it from there.
tell application "Finder"
set selectedFile to item 1 of (get selection)
set selectedFile to selectedFile as text
end tell
do shell script "curl -switchesToUpload " & quoted form of POSIX path of selectedFile
I use Cyberduck which is an ftp client you can set it up so when you double click a file on the server it opens it up in your favorite editor.( textmate is my favorite.) it atuomagiclly downloads and uploads when you save.
this seems like a much better solution to the problem