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.
Related
I'm a uni student taking online courses, I take portion screenshots by pressing Command+Control+Shift+4 every time to take notes, then I paste it to Notion. But it's not efficient to select the same area every time, then I found that QuickTime Player will keep the same area for capturing a portion screenshot, so I decided to use Apple Script to complete the operation.
The code I wrote could switch to the application QuickTime Player and then proceed to "New Screen Recording" in the menu by pressing Command+Control+N, but the problem is that sometimes pressing twice space will call out "Capture Selected Window" instead of "Capture Selected Portion". Meanwhile, the location where saving the screenshots will also be changed too though I already ticked "Remember Last Selection", maybe resetting the location to be saved every time is essential.
on run {input, parameters}
activate application "QuickTime Player"
tell application "System Events"
key code 45 using {command down, control down}
delay 0.1
# Switch to "Capture Select Portion" by pressing SPACE
repeat 2 times
key code 49
delay 0.1
end repeat
# Press ENTER to capture a screenshot
key code 35
delay 0.1
end tell
return input
end run
I spent a few hours finding the existed articles but perhaps not that fulfill my needs.
do shell script "screencapture -ci" is as same as pressing Command+Control+Shift+4, and I have to select the portion manually.
do shell script "screencapture -x -R20,20,640,380 ~/Desktop/test.png" needs a precise coordinate. But the coordinate is variable since the window is not fixed, and it needs to be found by using external software, maybe it's a little bit not efficient. If there is no solution for that, I will use it since it's much more convenient than selecting the portion every time.
Sorry about pasting others' code here and saying why they r not suitable for me, I just wanted to make it clear... Thanks for reading here, have a nice day no matter if u r willing to help me ^ ^
The part about pasting screenshots to Notion works, so I didn't paste it here.
I tried writing something, it works but still has some bugs.
For example, the coordinate of the menu "Options" should be initialized depending on the precise location of the bar since I didn't succeed in finding the element by using entire contents.
You can't move your mouse during the use, or it might cause an error:
System Events got an error: Can’t get menu \"Options\" of button \"Options\" of window \"Window\" of application process \"screencaptureui\"." number -1728 from menu "Options" of button "Options" of window "Window" of application process "screencaptureui
When selecting the path where screenshots will be saved, it might take a few seconds to let the program find the menu item Clipboard.
tell application "System Events"
tell process "QuickTime Player"
# Switch to QuickTime Player (already opened expected, no launching time reserved)
activate
# Open "New Screen Recording" from the menu
click menu item "New Screen Recording" of menu "File" of menu bar item "File" of menu bar 1 of application process "QuickTime Player" of application "System Events"
delay 1
end tell
# Capture Selected Portion
tell application process "screencaptureui"
click checkbox "Capture Selected Portion" of window "Window" of application process "screencaptureui" of application "System Events"
# Select Options from the menu (idk why if I replace it with click menu, the clipboard won't be found, needs to be fixed)
click at {817, 824}
delay 0.1
# Save to the Clipboard
click menu item "Clipboard" of menu "Options" of button "Options" of window "Window" of application process "screencaptureui" of application "System Events"
delay 1
end tell
# Enter to capture the screenshot
key code 36
delay 0.1
# Switch to Notion (already opened expected, no launching time reserved)
tell application "Notion"
activate
delay 0.1
end tell
# Paste the screenshot to Notion
key code 9 using {command down}
end tell
Hope this helps someone, if u hv any idea plz feel free to modify it since it still has bugs.
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 have many untitled TextEdit files. I'd like to use applescript to save each using, as a name, the text of the top line of each document.
The following will select and copy the first line of a document (not elegant, but it works), but I can't figure out how to paste the clipboard into the save dialog box (and hit "save" afterwards). Can anyone help?
tell application "TextEdit" to activate
tell application "TextEdit"
tell application "System Events" to key code 126 using command down
tell application "System Events" to key code 125 using shift down
tell application "System Events" to key code 8 using command down
end tell
There are 2 ways of doing:
1) the method using GUI scripting: this is what you've started to do. You simulate keyboard events like a user. It is not recommended for mainly 3 reasons: It is usually slow (you need to add delays to leave time for system open window, close them,..). During the script, if user hits key/mouse by mistake, your script will fail. And finally, you're hardly dependent of user interface of the application: if the editor (here Apple with TextEdit) changes something, like a short cut key, your script will no longer work.
Despite that, if you still want to use that way, here is the script that does it for you. I recommend that you add comments as I did (how to remember that key code 8 is 'c' !). I added some extra options to select the path to save (go home folder, enter special path,...). Up to you to use them or not:
tell application "TextEdit"
activate
tell application "System Events"
key code 126 using command down -- command up (cursor at start)
key code 125 using shift down -- shift down (select 1st line)
keystroke "c" using command down -- command C (copy)
keystroke "s" using command down -- open save dialog
delay 0.5 -- to let save as dialog time to open
keystroke "v" using command down -- paste the title from clipboard
-- other options
-- keystroke "h" using {command down, shift down} -- go home directory
delay 0.5
keystroke "g" using {command down, shift down} -- go to dialog
delay 0.5
keystroke "Desktop/Sample" -- path from Documents folder to Sample folder on Desktop
delay 0.5
keystroke return -- close the go to dialog
delay 0.5
keystroke return -- close the save as dialog
end tell
end tell
2) the method using Applescript instructions. It is usually much shorter, more elegant script, much faster to run, and user can't break it during execution. The script bellow does same as script above: It selects the first text row and save the document with that title. Line 1 defines the folder where to save:
set myPath to (path to desktop folder) as string -- path where to save file
tell application "TextEdit"
activate
tell front document
set myTitle to first paragraph
set myTitle to text 1 thru -2 of myTitle -- to remove the return at end of paragraph
save in (myPath & myTitle)
end tell
end tell
I hope it helps
I can't manage to find how to use the selected text as a variable for AppleScript and Automator.
Any ideas?
For Applescript, it works with other applications. To get the selected text of the front window in an app, Applescript has to use the language/syntax that this app understands/responds to. For very scriptable, text document based apps, there is much similarity, looking something like:
tell app "xyz" to get selection of document 1
However, there really is no standard. Many apps don't have a 'text selection' object in their scriptable dictionary, so you have to do all kinds of workarounds. See these examples:
tell application "Safari" to set selectedText to (do JavaScript "(''+getSelection())" in document 1)
tell application "System Events" to tell application process "TextEdit" to tell attribute "AXSelectedText" of text area 1 of scroll area 1 of window 1 to set selectedText to its value
tell application "Microsoft Word" to set selectedText to content of text object of selection
You can also script "System Events" to simulate the keystroke of command-c in order to copy text.
tell application "System Events" to keystroke "c" using {command down}
delay 1
set selectedText to the clipboard
If you need more specific help, post your code and indicate what app you are working with. If it is not a scriptable app, then you'll have to use the last method, calling System Events. Or, it's possible you can use an OS X Service, which you also asked about.
When you create a Service in Automator, you create a new Workflow of the type Service. Then, simply make sure that at the top of the window it says:
"Service receives selected text".
You can then use Automator actions to interact with the selected text that gets passed to the actions that follow.
Not all programs are compatible with Services, unfortunately.
To see how it works, try this very simple Automator service:
Create a Service in Automator and choose Text and Every application as input.
The first workflow step is Execute Applescript.
The Applescript's input parameter contains the selected text.
Set the Applescript to
on run {input, parameters}
display dialog (input as text)
return input
end run
After saving, you will have this action available in the context menu whenever you have selected text.
Maybe the naming is different, I don't know the English descriptions. But I hope this is a good starting point for you.
Have fun, Michael / Hamburg