I have an AppleScript exported as a .app file because I need it to run on log in.
This is my code:
repeat
tell application "System Events"
repeat with desktopNumber from 1 to count of desktops
tell desktop desktopNumber
set picture to "~/Desktop/script/img.jpg"
end tell
end repeat
end tell
end repeat
The path to the script is ~/Desktop/script/script.app/Contents/Resources/Scripts/main.scpt
I'd like to put the image in the Resources folder as well and make the path relative so i can put the folder anywhere without changing my script so I tried
set desktopPicture to ((container of container of (path to me)) as text) & "/img.jpg"
repeat
tell application "System Events"
repeat with desktopNumber from 1 to count of desktops
tell desktop desktopNumber
set picture to desktopPicture
end tell
end repeat
end tell
end repeat
But that gives me the error Can’t make container of container of alias \"Macintosh HD:Users:Me:Desktop:script:script.app:Contents:Resources:Scripts:main.scpt\" into type text.
System Events is not able to expand the tilde ~.
If you want to refer to ~/Desktop/script/img.jpg using relative paths you could use
tell application "System Events" to set desktopPicture to file "img.jpg" of folder "script" of desktop folder
or
set desktopPicture to alias ((path to desktop folder as text) & "script:img.jpg")
Consider that in both cases AppleScript will throw an error if the file does not exist.
Related
Why does this work:
tell application "Finder"
activate
reveal POSIX file ("/Users/Torben/Library/Mobile Documents/com~apple~CloudDocs/MyFolder/file.png")
end tell
...but not this
tell application "Finder"
activate
reveal POSIX file ("/Users/Torben/Library/Mobile Documents/com~apple~CloudDocs/MyFolder/" & "file.png")
end tell
And how do I get it to work if I want to join a path (string) with a variable (string)?
System Events handles POSIX paths a lot better, but this is just another one of those AppleScript oddities. POSIX file will work outside of a Finder tell statement:
set x to POSIX file (pathVariable & otherPathVariable)
tell application "Finder"
activate
reveal x
end tell
but within a Finder tell statement you need to use it as a coercion:
tell application "Finder"
activate
reveal (pathVariable & otherPathVariable) as POSIX file
end tell
I recommend to use relative HFS paths. The first line points to the library folder of the current user.
set libraryFolder to path to library folder from user domain as text
tell application "Finder"
reveal file (libraryFolder & "com~apple~CloudDocs:MyFolder:" & "file.png")
end tell
try this to combine strings
reveal POSIX file (("/Users/Torben/Library/Mobile Documents/com~apple~CloudDocs/MyFolder/" & "file.png") as text)
this is the script I'm trying to use:
on open thisItem
set this_folder to (the POSIX path of thisItem)
set export_folder to "Macintosh HD:Users:j****.*******:Desktop:AUTOMATOR:export"
set pdf_folder to "Macintosh HD:Users:j****.*****:Desktop:AUTOMATOR:PDF"
tell application "Adobe Illustrator" to open thisItem
tell application "Adobe Illustrator" to save current document in export_folder as pdf with options {class:PDF save options, PDF preset:"Low res proofing"}
tell application "Adobe Illustrator" to close current document
tell application "UltraLowPDF" to open
tell application "Finder" to move every file of folder "Macintosh HD:Users:j*****.*****:Desktop:AUTOMATOR:PDF" to container of this_folder
end open
This script should do a few things:
1. It creates a pdf file from an .ai file using adobe illustrator.
2. It then runs that pdf file through an automator workflow, which then spits out a pdf into a folder on my desktop.
3. It then moves that pdf file from the desktop, back to the folder of the original .ai file.
Every part of the script works fine except for step 3, it just seems to ignore it entirely. I've tried adding a delay, thinking the script was getting ahead of itself.
I've also tried isolating the move part of the script, using this droplet:
on open thisItem
set this_folder to (the POSIX path of thisItem)
tell application "Finder" to move every file of folder "Macintosh HD:Users:j****.******:Desktop:AUTOMATOR:PDF" to container of this_folder
end open
But this just throws up an error saying that it can't get the «class ctnr» of the file dropped on it (-1728).
Any help is greatly appreciated.
Thanks.
(note: I've starred out the user name part of the filepath for privacy)
EDIT: It seems that it's the Automator part of the script that is the problem, (the ''UltralowPDF"" app). After this runs, nothing after it in the script will run.
The problem is that thisItem is actually theseItems (aka a list). Either you need a repeat loop or – as in the following code – get the first item of the list.
The Finder does not accept (slash separated) POSIX paths. It works only with (colon separated) HFS paths.
The script uses the relative path path to desktop which point always to the desktop of the current user.
on open theseItems
set automatorFolder to (path to desktop as text) & "AUTOMATOR:"
set thisItem to item 1 of theseItems
set export_folder to automatorFolder & "export:"
set pdf_folder to automatorFolder & "PDF:"
tell application "Adobe Illustrator" to open thisItem
tell application "Adobe Illustrator" to save current document in export_folder as pdf with options {class:PDF save options, PDF preset:"Low res proofing"}
tell application "Adobe Illustrator" to close current document
tell application "UltraLowPDF" to open
tell application "Finder" to move every file of folder pdf_folder to container of thisItem
end open
I'm trying to launch a Finder window of a folder that's in the same directory as my script. When I run the code below, it launches a blank Finder window set to the path of the script not to the folder.
tell application "Finder"
tell application "Finder" to make new Finder window
set file_path to (path to me) as text
set target of Finder window 1 to file_path
end tell
How can I get the path to the folder of the script, not the script?
You were close. You do not need the text version of the file, you only need the file itself, then you can ask Finder for that file's container:
tell application "Finder"
tell application "Finder" to make new Finder window
set file_path to (path to me)
set target of Finder window 1 to file_path's container
end tell
The shortest way I know to do this is:
tell application "Finder" to open ((path to me as text) & "::")
Editing your script renders the following:
tell application "Finder"
make new Finder window -- There is no need for an your second tell statement
set file_path to (path to me as text) & "::" -- Goes up one directory
set target of Finder window 1 to file_path
end tell
I'm attempting to set the current desktop picture to an image in the current directory of the script. For example, if my folder structure is:
~/Documents/Scripts/DesktopImageScript/image.jpg
With the script in the same directory, I want to be able to set the desktop image to the image.jpg without directly referring to the folder structure. The code I am currently using to fetch the current directory is:
tell application "System Events" to set app_directory to POSIX path of (container of (path to me))
The issue isn't in that code as I can run the following command with the expected and correct results:
do shell script "echo " & app_directory
I believe the issue is in the code I'm using to set the desktop image:
tell application "Finder"
set desktop picture to POSIX file (quoted form of POSIX path of (app_directory & "/image.jpg"))
end tell
The error I receive when I try to run the script is:
error "Finder got an error: AppleEvent handler failed." number -10000
Not really sure what could be causing the error or how to fix it. Any help is appreciated. The full script is below:
tell application "System Events" to set app_directory to POSIX path of (container of (path to me))
tell application "Finder"
set desktop picture to POSIX file (quoted form of POSIX path of (app_directory & "/image.jpg"))
end tell
Solved by using code from another answer on StackOverflow.
tell application "System Events"
set theDesktops to a reference to every desktop
repeat with x from 1 to (count theDesktops)
set picture of item x of the theDesktops to app_directory & "/image.jpg"
end repeat
end tell
on run {input}
set filepath to POSIX path of input
do shell script "touch " & quoted form of filepath & "untitled"
return input
end run
Is what I have so far, and it works, but is there a way to then focus on the file then trigger a rename? I dont want the rename to be automatic, just trigger the event (like pressing "return" while you have a file selected). And I dont want to use any sort of modal...
Quick Side question: is there a way to set this so that i dont have to select a folder or file directly, but can do it by, lets say, clicking in a white space in a folder as long as it's in Finder? Right now I have my "Service receives selected" to "files or folders" in Finder.app.
== UPDATED CODE ==
on run {input}
set filepath to POSIX path of input
do shell script "touch " & quoted form of filepath & "untitled"
tell application "Finder"
activate
set target of Finder window 1 to POSIX file "/Users/oscargodson/Documents/designs/untitled"
end tell
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
return input
end run
If i hardcode the path it works! But how do I get it as a var that works?
Here's one way. I think a modal window where you ask for the name would be better but you can try this. Notice you do not use "POSIX path" in this code. Applescript doesn't use POSIX paths. Also {input}, as indicated by the brackets around it, is a list of items. Therefore you act on the items of the list, and in this case we act on the first item.
set filepath to item 1 of input
tell application "Finder"
activate
reveal filepath
end tell
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
EDIT: With your updated code, here's a working script...
on run {input}
if (class of input) is not list then set input to {input}
set theFolder to (item 1 of input) as text
try
alias theFolder
tell application "Finder"
if (class of item theFolder) is not folder then error "input is not a folder."
activate
set theFile to make new file at folder theFolder with properties {name:"untitled"}
reveal theFile
end tell
delay 0.2
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
on error theError number errorNumber
tell me
activate
display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
return
end tell
end try
return input
end run
tell application "Finder"
activate
reopen -- in case there are no open windows
set target of Finder window 1 to POSIX file "/Applications/Safari.app"
end tell
reveal and select always open a new window, set target and set selection don't.
I don't know why, but when set selection it used in column view, you can only select items in the entire contents of the target of the front window. The same thing doesn't happen in other views, so it seems like a bug.
Fix for the code in the edited question:
on go(input)
set p to POSIX path of (input as text)
set p2 to p & "untitled"
do shell script "touch " & p2
tell application "Finder"
reopen
activate
set target of Finder window 1 to POSIX file p2
end tell
delay 0.3 -- time to release modifier keys
tell application "System Events" to keystroke return
end go
tell application "Finder"
set fold to folder (path to documents folder)
end tell
go(fold)
(That on go and the last lines are just for testing.)
I've created an AppleScript based on the #regulus6633's one, but with some improvements.
Note: This answer was originally posted as an AskDifferent answer. I'm copy/pasting here for convenience.
The idea is to create an Automator workflow and assigning a shortcut to it using the following steps:
Open Automator and create a Service;
Set the input to no input, and the application to Finder.app;
Drag and Drop the Run AppleScript workflow element onto the grey space;
Put the contents of this AppleScript in the textbox;
Save the workflow with a reasonable name (like New File);
Go to Settings -> Keyboard -> Shortcuts -> Services and assign a shortcut to it.
Now, let's show the AppleScript:
set file_name to "untitled"
set file_ext to ".txt"
set is_desktop to false
-- get folder path and if we are in desktop (no folder opened)
try
tell application "Finder"
set this_folder to (folder of the front Finder window) as alias
end tell
on error
-- no open folder windows
set this_folder to path to desktop folder as alias
set is_desktop to true
end try
-- get the new file name (do not override an already existing file)
tell application "System Events"
set file_list to get the name of every disk item of this_folder
end tell
set new_file to file_name & file_ext
set x to 1
repeat
if new_file is in file_list then
set new_file to file_name & " " & x & file_ext
set x to x + 1
else
exit repeat
end if
end repeat
-- create and select the new file
tell application "Finder"
activate
set the_file to make new file at folder this_folder with properties {name:new_file}
if is_desktop is false then
reveal the_file
else
select window of desktop
set selection to the_file
delay 0.1
end if
end tell
-- press enter (rename)
tell application "System Events"
tell process "Finder"
keystroke return
end tell
end tell
For convenience, I'm putting this AppleScript in this GitHub Gist.