Applescript interacting with another applications file selection - applescript

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

Related

How to select file using AppleScript in Finder prompt?

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.

AppleScript to click Wi-Fi icon with option down

I would like to click on the Wi-Fi icon with the option key down to reveal extra options available on Mac. How can I automate it using AppleScript?
I tried using key down option and click menu item but no luck in revealing special options.
Is there any way I can achieve this?
It's currently not possible to click with a key held down using AppleScript. Key down actions only apply to other key press actions, since the AppleScript click action doesn't actually perform a ‘click’, but rather directly actions the element.
If you don't mind using a 3rd party utility, here's an example AppleScript script that uses cliclick:
tell application "System Events"
tell application process "SystemUIServer"
set theWiFiProperties to item 1 of (get properties of every menu bar item of menu bar 1 whose description starts with "Wi-Fi")
end tell
set theXpos to (item 1 of position in theWiFiProperties) + ((item 1 of size in theWiFiProperties) / 2) as integer
set theYpos to (item 2 of position in theWiFiProperties) + ((item 2 of size in theWiFiProperties) / 2) as integer
end tell
tell current application
do shell script "/path/to/cliclick kd:alt c:" & theXpos & "," & theYpos & " ku:alt"
end tell
Note: Change /path/to/cliclick to the actual pathname of the cliclick executable.
How it works:
The theWiFiProperties variable gets set to the properties of the Wi-Fi menu extra and then the variables theXpos and theYpos get set to a position that together is the center of the Wi-Fi menu extra on the menu bar.
This info is then used in a do shell script command using cliclick to press the option key down, click at the designated x,y coordinates and let the option key up.
You can use Automator and record the process using “Watch me do” and then save the automated workflow as an application or a dictation command.
In Automator, I saved the watch me do action as an application. I named this new application “Extended_Wifi.app”. Then I had to add this application in system preferences to be able to control my computer.
Personally, I prefer to use Scripteditor rather than Automator because a huge part of me feels like using Automator is cheating. But at the end of the day, I was able to save the Automator action as an application and it functions perfectly however in Scripteditor, I Could not get the AppleScript version of the action to function correctly.
Here is a quick .gif showing the Automator application working correctly.

Applescript to Control Photoshop Save Dialog Box

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

AppleScript performing a click while Command (or another modifier) is pressed

I know you can perform keyboard shortcuts with AppleScript like this:
-- to perform Cmd + P
tell application "System Events"
keystroke "P" using {command down}
end tell
Now I would like to take it a step further. I would like to perform a mouse click while a modifier is down.
I tried the following:
-- to perform Cmd + click
tell application "System Events"
key down command
tell application process "Some application" to tell button "SomeButton" to click
key up command
end tell
But this doesn't yield the same result as physically performing the Cmd + click.
How can I achieve my goal?
Indeed it may be better to script this directly, if possible. However, if it is not possible you can always click on any point on the screen while holding down a modifier key. For example:
do shell script "cliclick kd:cmd c:28,11"
will click on screen coordinates (28, 11) while holding down the command key. The screen coordinates you want to click can be manually identified with your cursor by holding down command + shift + 4. To make the above script work you will need a copy of the free Terminal/Command Line app cliclick.
It would be easier to try to think about what event you are trying to trigger instead of using the mouse to do it. Afraid I can't be of more help, but that would be my approach.

Is there a way to trigger a Hot Key/Keyboard Shortcut via a Shell Script, a AppleScript or a Automator Workflow?

I'm an avid Keyboard Maestro user and I need a workaround for triggering a keyboard shortcut like ⌘⇧L (externally, without Keyboard Maestro). So I thought a bash script would be capable of doing such a thing. An AppleScript or an Automator workflow would be sufficient, too. I anybody could help me this would be great.
You don't have to read this, but here's why I want to do what I want to do:
I have a the same string assigned to various Markdown macros, I use a string instead of Hotkeys because it's much more memorable for me since my brain already is filled with so many application shortcuts. The disadvantage is that Keyboard Maestro won't delete the keystrokes of the string. I can perform several actions within the program to delete them but adding these actions for each and every macro is tedious and suboptimal.
tell application "System Events" to keystroke "l" using command down & shift down
tell application "System Events"
key code {123, 124} using {shift down, command down} -- ⇧⌘←, ⇧⌘→
keystroke "c" using command down -- keystroke "C" would be treated as ⇧C
end tell
delay 0.02 -- you need a small delay here before the next command
set txt to Unicode text of (the clipboard as record)
Reference of Mac key codes: lri.me/chars

Resources