How are you?
I've created an applescript automation to save files in .JPG while the Save Dialog Box is open. (So I can control the name of the saved files)
Is there a way to control the Save Dialog Box of Photoshop?
What I want to happen is: Upon appearing of save dialog box
-Command + a will happen (to select all characters)
-Press delete (to delete all characters selected)
-Delay 8 seconds = Enough time for me write my own file name.
-Automation will press return to save the file under my own written file name.
I tried reading Photoshop's dictionary at Script editor but found no results for Controlling Photoshop's save dialog box.
I tried doing system events to do command a + press delete + delay 8
seconds and pressing return but that event only happens after the save
dialog box disappears instead of doing that on the actual save dialog
box.
My Photoshop is: CS6 Extended
Os: El Capitan
Thank you very much.
You should avoid using GUI scripting : each time Adobe (or Apple) will change the graphic display of the 'save as' dialog box, your script may no longer work.
Instead, use a 2 step approach : 1) get the false name and path using a standard 'choose file name' and then use this file to save using 'save' command in Photoshop. This script assume there is a current open document.
Please update 'Adobe Photoshop CS3' with your version (mine is a bit old, but good enough to test !).
Also, the default folder could be adjusted for your needs (here = Desktop).
tell application "Adobe Photoshop CS3"
set docRef to the current document
set docName to name of docRef -- current name will be use as default new name
set file2save to ((choose file name default location (path to desktop) default name docName) as string)
save docRef in file file2save as JPEG appending lowercase extension with copying
end tell
Note 1: you can improve that script by checking the extension typed in file2save variable, and, if missing, the script can add the correct extension (i.e. 'jpg').
Note 2: Adobe made some changes in 'open ' command between version CS3 and CS6. I hope these changes do not affect the 'save' command.
This is a code to what you specified also it includes open the save box:
tell application "Adobe Photoshop CS6"
activate
tell application "System Events"
keystroke "s" using {command down, shift down}
delay 1
keystroke "a" using {command down}
delay 0.1
key code 51
delay 8
keystroke return
end tell
end tell
Related
How do I copy the result of the Calculator.app including decimals.
By defaults is selected, so if you just do a CMD+C it copies it into your clipboard. I got this code from #jweaks
set the clipboard to {?????}
I tried entering many different options but I don't know what I should put.
How do I copy the result of the Calculator.app including decimals.
set the clipboard to {?????}
As you already know ⌘C can do it, however, if you want to use a set clipboard to method, then here is one way to go about it:
Example AppleScript code:
if not running of application "Calculator" then return
tell application "System Events" to ¬
set the clipboard to ¬
(get the value of ¬
static text 1 of ¬
group 1 of ¬
window 1 of ¬
process "Calculator")
Notes:
Does not require Calculator to be frontmost.
Does not require the use of keystroke or key code to accomplish the task.
Can set the value to a variable instead of the clipboard, if wanting to process it in a different manner.
The example AppleScript code, shown below, was tested in Script Editor under macOS Catalina and macOS Monterey with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
In testing, the Replies pane in Script Editor returned, e.g.,:
tell application "System Events"
get value of static text 1 of group 1 of window 1 of process "Calculator"
--> "6200.549407114624506"
set the clipboard to "6200.549407114624506"
end tell
Then when pasting into a document it pasted as: 6200.549407114624506
Update to address comments
To address the ensuing comments by sebseb under my answer and specifically…
Is it possible to run the script every time I hit enter on Calculator? then copy the result.
Basic vanilla AppleScript is not that intelligent and does not have the ability in of and by itself to understand what one is doing in Calculator and know when one has pressed the enter key to then place the result on the clipboard.
One would have to use an intermediary, an application like Hammerspoon, where it can wait for the Calculator application being activated/deactivated or its window being focused/unfocused to then enabled/disable trapping the enter key being pressed on the keyboard to then run the script to perform an action to calculate the result by pressing the = key then copy the result to the clipboard.
This works because pressing the = key in Calculator is equivalent to pressing the enter key, thus enabling trapping the enter key to perform the necessary actions using AppleScript. It quite possibly can be done without using AppleScript and just Lua, the language used by Hammerspoon and its API. However, since I already use various AppleScript scripts in conjunction with Hammerspoon and can easily recycle some existing code I'll present an addendum to the original answer using both methods in Hammerspoon.
The following example Lua code and API of Hammerspoon is placed in the ~/.hammerspoon/init.lua file.:
-- Create a hotkey used to trap the enter key and disable it.
-- It will then be enabled/disabled as Calculator is focused/unfocused
-- When enabled and the enter key is pressed it runs the AppleScript script.
local applicationCalculatorEnterHotkey = hs.hotkey.bind({}, "return", function()
local asFile = "/.hammerspoon/Scripts/CalculatorResultToClipboard.applescript"
local ok, status = hs.osascript.applescriptFromFile(os.getenv("HOME") .. asFile)
if not ok then
msg = "An error occurred running the CalculatorResultToClipboard script."
hs.notify.new({title="Hammerspoon", informativeText=msg}):send()
end
end)
applicationCalculatorEnterHotkey:disable()
-- One of two methods of watching Calculator.
--
-- The other is below this one and commented out.
-- Initialize a Calculator window filter.
local CalculatorWindowFilter = hs.window.filter.new("Calculator")
-- Subscribe to when the Calculator window is focused/unfocused.
CalculatorWindowFilter:subscribe(hs.window.filter.windowFocused, function()
-- Enable hotkey when Calculator is focused.
applicationCalculatorEnterHotkey:enable()
end)
CalculatorWindowFilter:subscribe(hs.window.filter.windowUnfocused, function()
-- Disable hotkey when Calculator is unfocused.
applicationCalculatorEnterHotkey:disable()
end)
-- Alternate method to wait for Calculator and enable/disable the hotkey.
--
-- Uncomment below method and comment the above method to test between them. Adding the
-- multiple line opening '--[[' and closing '--]]' to above method and removed from below,
-- leaving 'local CalculatorWindowFilter = hs.window.filter.new("Calculator")' uncommented.
--[[
function applicationCalculatorWatcher(appName, eventType, appObject)
if (eventType == hs.application.watcher.activated) then
if (appName == "Calculator") then
-- Enable hotkey when Calculator is activated.
applicationCalculatorEnterHotkey:enable()
end
end
if (eventType == hs.application.watcher.deactivated) then
if (appName == "Calculator") then
-- Disable hotkey when Calculator is deactivated.
applicationCalculatorEnterHotkey:disable()
end
end
end
appCalculatorWatcher = hs.application.watcher.new(applicationCalculatorWatcher)
appCalculatorWatcher:start()
-- appCalculatorwWatcher:stop()
--]]
The following example AppleScript code is used in conjunction with Hammerspoon and is saved as CalculatorResultToClipboard.applescript in ~/.hammerspoon/Scripts/, and you'll need to create the hierarchical folder structure.
Example AppleScript code:
One can use either:
tell application "Calculator" to activate
tell application "System Events"
key code 24
delay 0.5
set the theResult to the value of static text 1 of group 1 of window 1 of process "Calculator"
end tell
set the clipboard to theResult
Or:
tell application "Calculator" to activate
tell application "System Events"
key code 24
delay 0.5
key code 8 using command down
end tell
To accomplish the task.
An alternate option, as previously mentioned, is to forgo the use of AppleScript and use the following example Lua code:
local applicationCalculatorEnterHotkey = hs.hotkey.bind({}, "return", function()
-- Press the '=' key to finish the calculation.
hs.eventtap.keyStroke({}, "=")
-- Copy the result to the clipboard.
hs.eventtap.keyStroke({"cmd"}, "C")
end)
applicationCalculatorEnterHotkey:disable()
This function would be used instead of the same function further above. It replaces the execution of the AppleScript script with keystrokes generated by Hammerspoon to accomplish the same tasks, while using the remaining example Lua code and the API of Hammerspoon already presented.
Notes:
With the example Lua code, as coded, the behavior of pressing the enter key is only trapped and modified to trigger the example AppleScript code, or if using the alternate option send Hammerspoon keystrokes, while Calculator has focus. The enter key should work normally in all other applications.
See my other Hammerspoon related answers for instructions to install it and utilize the information contained herein.
One in particle is:
A: How can I make preview stop wrapping around when paging?
If using Script Editor, the example AppleScript code is saved as Text in the File Format: pop-up menu in the Save dialog box.
The example Lua code and API of Hammerspoon and AppleScript code, shown directly above, were tested respectively with Hammerspoon and Script Editor under macOS Mojave and macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Calculator.app doesn't have an AppleScript dictionary.
You have to script the UI with System Events
tell application "System Events"
tell process "Calculator"
set frontmost to true
keystroke "c" using command down
end tell
end tell
I am working with Selenium on macOS to automate sending images using WhatsApp web in Google Chrome. The task involves uploading the image, and for that a system(Finder) prompt comes up to select the file. It's done in Windows using AutoIt.
I tried looking up how to automate this task in macOS, and I believe AppleScript can be used for it. Since I have no experience in GUI scripting, any help would be appreciated.
Thanks.
I was able to find the answer on another post on Stack Overflow. I have added the answer for anyone who comes across the same problem.
tell application "System Events"
keystroke "G" using {command down, shift down}
delay 1
keystroke "/path/to/file"
delay 1
keystroke return
delay 1
keystroke return
delay 1
end tell
I don't advocate GUI scripting any more than the burning down of the Amazon, but it seems to be necessary for this task, and I wanted to provide you with an example of a GUI script that tries its best to minimise the unpleasantness of the user experience, and aim for fewer weak points in the code where GUI scripts are most likely to falter.
If you know the path to your file—which I assume you do in these sorts of situations, as your script keystrokes the filepath—then you might find the following technique saves a few steps, and feels a bit more graceful in how it gets executed:
set filepath to "/path/to/image.jpg"
-- Copy file object to clipboard
set the clipboard to filepath as «class furl»
-- Make sure Chrome is in focus and the
-- active tab is a WhatsApp tab
tell application id "com.google.Chrome"
activate
if the URL of the active tab in the front window ¬
does not contain "web.whatsapp.com" then return
end tell
-- Paste the clipboard contents
-- and hit return (send)
tell application id "com.apple.SystemEvents"
tell (process 1 where it is frontmost) to tell ¬
menu bar 1 to tell menu bar item "Edit" to tell ¬
menu 1 to tell menu item "Paste" to set Paste to it
if (click Paste) = Paste then keystroke return
end tell
The if (click Paste) = Paste check should negate the need for a delay, as it explicitly forces AppleScript to evaluate the click command before going on to issue a keystroke. However, I can't test this under all possible conditions, and if there are other factors, like CPU usage, or process freezes, that are likely to give the script a chance to jump ahead, then just insert a small delay after then and move keystroke return down onto its own line.
If you wish to remove the file object from the clipboard afterwards, then simply add as the final line set the clipboard to (and just leave it blank after the word "to", which will clear the clipboard's contents). Of course, this won't affect any clipboard history data you might have if you use a clipboard managing app, only the system clipboard's current item.
I have an applescript that I can open up an application, enter the password, and get to a file selection dialog. I am wondering if it is possible to interact with the standard mac file dialog if i have my file path.
Since this is interacting with another developers application, I cannot change this dialog in anyway.
I can get to the search field by "tabbing" over and searching for the file, it is unique with a date/time stamp, but have no way of selecting it and the hitting open.
Found via a different website for anyone who may lurk later on. Since it uses the standard apple window you can use the keyboard short cut shift + cmd + g to get the "go to folder" option from finder, from there entering the file path and hitting return. Had to add in delays on the two returns or it may miss the second one.
tell application "System Events"
keystroke "g" using {shift down, command down}
keystroke "Your File Path Goes Here!"
delay 1
key stroke return
delay 1
key stroke return
I use firefox for a long time as my only browser on Pc or Mac.
In a few words my problem: I want to create a service on mac with automator and
Applescript for quite instant translation using translate.google.com.
What works great with Safari or Chrome (below the 4 or 5 lines of script)
On run {input, parameters}
Tell application "Safari"
activate
try
Open location "https://translate.google.com/#auto/en/" & (the clipboard)
end try
end tell
end run
The same thing (script) does not work at all with Firefox, I try by different ways
To circumvent the impossible problem
On run {input, parameters}
Set theProcess to "Firefox"
Set info to {}
Set y to 0
Set x to 0
Set n to 0
Tell application "Applications / Firefox.app"
activate
Open location "http://translate.google.com/#auto/en/"
end tell
Tell application "System events"
Repeat with theProcess in (process "Firefox")
try
Set info to info & (value of (first attribute whose name is "AXWindows") of theProcess)
end try
end repeats
Set n to count of info
info
end tell
Tell application "System Events" to tell process "Firefox"
Set x to "1"
Set frontmost to true
try
Click menu item "Paste" of menu "Edit" of menu bar 1
end try
Repeat while x is "1" -
If x is "1" then
Keystroke "V" using command down
Set x to "0"
end if
end repeat
end tell
end run
By copying and pasting, the actions take place before the complete loading of the page, even by slowing down the Copy and Paste procedure.
After many observations there is a problem of formatting the text contained in the clipboard with the association of the URL, I improved this but it is not perfect yet.
tell application "Applications/Firefox.app" to activate
tell application "System Events" to tell process "Firefox"
set frontmost to true
set sentence to text of (the clipboard)
set thesentences to paragraphs of sentence
set thenewsentences to thesentences as string
set the clipboard to thenewsentences
keystroke "t" using command down
keystroke "https://translate.google.com/#auto/fr/" & (the clipboard) & return
end tell
Anyway if it works with Safari without modifying anything, the problem is at the Firefox entry, so if you can look at this problem, that would be very useful to us all.
Thank you for your attention.
Thank you for your answers.
Safari and Chrome perform the necessary encoding of reserved characters in the URL for you, but Firefox doesn't.
Therefore, you need to perform the encoding of the query-string value (the text to embed in the URL) explicitly.
The easiest (though not obvious) approach is to use perl, via a shell command, gratefully adapted from here:
# Example input that contains chars. that have special meaning in a URL ('&' and '?')
set the clipboard to "Non, Je ne regrette rien & rien ne va plus?"
# Get text from the clipboard and URL-encode it.
set encodedText to do shell script ¬
"perl -MURI::Escape -lne 'print uri_escape($_)' <<<" & quoted form of (the clipboard)
# Now it's safe to append the encoded text to the URL template.
tell application "Firefox"
activate
open location "https://translate.google.com/#auto/en/" & encodedText
end tell
The above approach works with all three browsers mentioned: Firefox, Safari, and Google Chrome.
Note:
As of (at least) Firefox v50, Firefox opens the URL in in a new tab in the current front window by default.
You can make Firefox open the URL in a new window instead, by unchecking Open new windows in a new tab instead on the General tab of Firefox's preferences.
Note, however, that this is a persistent setting that affects all URLs opened from outside of Firefox.
For an ad-hoc solution for opening in a new window that doesn't rely on changing the setting, see this answer of mine.
Hello below the service Automator with some version it is possible to encounter problems, I modified the script so that it works almost everywhere.
A frequent system error is the permission to applications to control your computer that is handled by the system preferences tab Security and Privacy, the system asks if you allow Firefox, TexEdit and others using this service for its keyboard shortcuts.
Also in Automator create service (to be General (and appear in all applications) no entry (up to Yosemite since El Capitan I saw that with Firefox for example all the services are usable), choose Execute a script Applescript paste the script below divided into 2 script or 1 script only.
on run
set Sn to ""
tell application "System Events"
set Sn to the short name of first process whose frontmost is true --here we look for and find which application to launch the service
tell process Sn
set frontmost to true
try
click menu item "Copy" of menu "Edit" of menu bar 1 -- there is used the Copier of the menu Editing of the application
end try
end tell
end tell
return the clipboard
end run
In the script following the entry is done with the contents of the Clipboard
On run {input}
on run {input}
set input to (the clipboard) -- Here we paste the contents of the Clipboard into a variable
try
set input to do shell script "Perl -MURI :: Escape -lne 'print uri_escape ($ _)' <<< " & quoted form of input --To format the text to make it usable with Firefox and the url of translate.google.com
tell application id "org.mozilla.firefox"
activate
open location "https://translate.google.com/#auto/en/" & input --here since version 50 of Firefox you must open a tab and not a new url window of translate.google.com, with option #auto automatic detection by google of the language, and fr to translate into French (these options are modifiable at will)
end tell
end try
end run -- end of the operation you have a tab open on translate, a text to translate and a translated text
I am unsuccessfully trying to use AppleScript to automate the process of making small edits to a bunch of files. More specifically, I want a script that will:
Open a specific file in QuickTime
Split it into segments of a specified length
Save each segment as an individual file in the same format and with the same quality as the original.
Close the document
Most importantly, I want the script to essentially work unassisted/unmanned.
Here's some more info on what I'm trying to do: http://hints.macworld.com/article.php?story=20100305070247890.
Another user on StackOverflow asked a similar question a while back, but the suggestion does not work.
From the few online discussions I've been able to find, it appears that Apple took away some of the functionality of QuickTime after version 7. I'm currently using 10.3+
Here's another discussion that describes almost exactly what I'm trying to do. As "kryten2" points out, export no longer seems to work in the new version of QuickTime. And, just like "VideoBeagle", I get permissions errors when I try to call the save method.
The code posted by VideoBeagle on that page does not work for me. Here's a modified version:
tell application "QuickTime Player"
open basefile --argument passed to script when executed
set the clipboard to "outputfile"
delay (0.25)
tell document 1
trim from 0 to 60
tell application "System Events"
tell process "QuickTime Player"
keystroke "s" using command down
keystroke "v" using command down
delay 1
keystroke return
delay 3
#click menu item "Save..." of menu "File" of menu bar 1
end tell
end tell
close saving no
end tell
end tell
The code above DOES open the file in QuickTime and trims the file to the correct length, but then it creates an unsaved copy of the file in a new window, closes the original, but does not save the new document. When I experiment with the delay and remove the "trim" function, it will show the Save dialog but won't actually save a file.
Has anyone successfully managed to use AppleScript and QuickTime to save files? ...recently?
Thank you so much!
The best should be to use export function of QuickTime Player 7 if you have the QuickTime Pro authorization (not free, but very cheap). to do so, you also need to download this old QT version from Apple site. it is still available, but Apple promotes the QuickTime Player 7 with basically only read functions.
Still if you want to stick to QuickTime Player (after version 7), there are known issues in scripting while saving. The workaround is to simulate part of GUI as you already start doing.
The script bellow asks for movie file to be processed, defines the new path and name for the modified video, trim from second 2 to second 6 and then use the GUI interface to save and close. I made many comments to make sure you can understand and update for your own needs :
-- select file to be processed
set myVideo to choose file with prompt "Select video to be processed"
-- set new path and file name
set newPath to ((path to desktop folder from user domain) as string) & "Test_Folder"
set PPath to POSIX path of newPath
set newName to "cutVideo.mov"
tell application "QuickTime Player"
activate
open myVideo
set myDoc to front document
trim myDoc from 2 to 6 -- keep only video from second 2 to 6
end tell
tell application "System Events"
tell process "QuickTime Player"
keystroke "s" using {command down}
delay 0.8
keystroke "G" using {command down} -- go to
delay 0.8
keystroke PPath -- folder path with / and not :
delay 0.8
keystroke return
delay 0.8
keystroke newName -- fill file name
delay 0.8
keystroke return -- ok save dialog
delay 0.8
keystroke "w" using {command down} -- close window
end tell
end tell