AppleScript Clicking On dialog box - macos

In PCSX, (ps1 emulator), i'm trying to automate the steps to play an iso. So, i'm doing this:
set thepath to path to me
set thesecondpath to POSIX path of thepath
set thethirdpath to "Contents/PSX/ROMS/img.bin"
set thefourthpath to "/Contents/PSX/PCSX.app"
set thefifthpath to thesecondpath & thefourthpath
set theultimatepath to thesecondpath & thethirdpath
tell application thefifthpath
activate
tell application "System Events"
keystroke "i" using {command down}
keystroke theultimatepath
delay 1.0
tell process "PCSX"
click button "Go"
end tell
key code 53
end tell
end tell
Running from the AppleScript Editor won't work. I made it to work running from the App it creates. PCSX and the img.bin are inside the Generated Package.
after pressing command+i, it opens a "Go to the folder" dialog, and i need to click Go and then Open
But doing this way, it won't find the dialog box. What am i doing wrong?

If Go and Open are the default buttons, try:
tell application "System Events"
keystroke return
delay 2
keystroke return
end tell
Although I don't have PCX installed here is an example of how to click the Go button from Finder's Go to Folder command.
tell application "System Events"
tell process "Finder"
click button "Go" of window "Go to Folder"
end tell
end tell

The reason your script won’t work from AppleScript Editor is that the “me” in “path to me” is the application that ran the AppleScript. When you are running the AppleScript in AppleScript Editor, that means AppleScript Editor itself. When you saved your AppleScript as a script application and ran it, the path to me pointed to your script application, because it was running its own AppleScript.
Also, this is incorrect:
tell process "Finder"
click button "Go" of window "Go to Folder"
end tell
The “Go” button is not on the window “Go to Folder.” It’s on a sheet which is attached to a Finder window which has the name of whatever folder is currently being viewed. So you have to describe the button as being on sheet 1 of window 1:
tell application "System Events"
tell process "Finder"
click button "Go" of sheet 1 of window 1
end tell
end tell
… but keep in mind that in another app, a similar looking button on a sheet may be on sheet 1 of group 1 of group 2 of window 3. UI Scripting is complicated.

Related

AppleScript - How to set the target/path of a finder window prompt when it's prompted from an App (not from the Finder)?

I am using AppleScript to automate a process on a GUI.
I use the script to click a button "Add Source Folder" (similar to opening a file in any apps) in the GUI, which prompts an integrated Finder window (image here).
In practice, once the prompted finder window opened, I'd like the path to be set automatically from an alias object theFolderToProcess's (which would've been set previously in the script).
I am unable to set its path because I have nowhere to set the alias theFolderToProcess within the in-app prompt finder window. So how do I get the script to navigate to the path of the alias?
Here is the code:
set theFolderToProcess to choose folder with prompt "Step 1: Select the folder to convert:"
tell application "System Events"
tell application process "MyApp"
tell its window "MyApp"
activate
click button "Add Source Folder"
set uiElems to UI elements
end tell
end tell
end tell
With UI Elements, I get sheet 1 of window "MyApp" of application process "MyApp" of application "System Events" where sheet 1 is the prompt window.
Note: Setting the path in the prompt window doesn't work.
From within the 'open' dialogue, you should be able to use the Finder's Go to Folder… function, or command-shift-G (or whatever it is on your OS). Then use keystroke to enter your desired location. The location reference should follow the format below rather than using your alias. Follow up with a key code to accept your location. I find the short delays help with keyboard scripting but edit as desired.
set posixFolder to posix path of theFolderToProcess
--> "~/Desktop/exports/"
delay 0.1
key code 5 using {command down, shift down} -- Go to folder…
delay 0.1
keystroke posixFolder -- folder must exist
--> keystroke "~/Desktop/exports/" -- folder must exist
delay 0.1
key code 76 -- type Return (Go button)
There is a full example here but this should be sufficient.
A don't know your "App", so following example is using TextEdit. If you successfully open OPEN dialog using other way (click button "Add Source Folder"...) then replace corresponding code line of my script with yours. My script shows how to set automated delays as well. And how to click the "Go" button at the end.
set processName to "TexEdit"
set sourceFolder to POSIX path of (path to pictures folder)
tell application processName to activate -- bring to front
tell application "System Events"
-- this is instead of your 'click button "Add Source Folder"'
-- it opens OPEN dialog window
keystroke "o" using command down
-- wait
repeat until window "Open" of process processName exists
delay 0.02
end repeat
-- open go to path sheet
keystroke "g" using {command down, shift down}
repeat until sheet 1 of window "Open" of process processName exists
delay 0.02
end repeat
-- keystroke full posix path
keystroke sourceFolder
-- go
click button "Go" of sheet 1 of window "Open" of process processName
end tell

With Applescript I need to use System Events to navigate a Finder Window that opens within an application

I'm currently writing some Applescript to import some material into software which doesn't have much applescript support and virtually no documentation on Applescript. The two methods are to somehow adapt this script to work:
on adding folder items to this_folder after receiving these_items
try
tell application "Isadora"
import media into document 1 from these_items
end tell
on error msg
display dialog "Error importing file into Isadora: " & msg
end try
end adding folder items to
However when I try to use "import media into document 1 from ________" I always get an error, in every incarnation of that combination.
My second approach then is using System Events to navigate through the menu bar to import everything. When I get to the import part of things where I can navigate and select the files, a finder window pops up. My question is:
How do I navigate this finder window within the application? This is like the inception of scripts. A finder window within a program.
I tried a few simple things like calling the front window of finder to navigate somewhere and what not. The current script that gets me to the import window is:
tell application "IsadoraCore"
activate
delay 2
tell application "System Events"
click menu bar item "File" of menu bar 1 of application process "IsadoraCore"
click menu item "Import Media..." of menu "File" of menu bar item "File" of menu bar 1 of application process "IsadoraCore"
end tell
end tell
Any help would be appreciated!
I would guess the "import media" window is a Finder window where you select a file to import. This window is easy to handle. To select the file you must have the posix path of the file. In that window if you keystroke "shift-command-g" then you get a text field where you can keystroke the posix path. For example, let's say I want to open a file in TextEdit. The file is on my desktop with the name test.txt. I can do this...
set filePath to (path to desktop as text) & "test.txt"
set posixPath to POSIX path of filePath
set aShortDelay to 0.5
tell application "TextEdit" to activate
delay aShortDelay
tell application "System Events"
keystroke "o" using command down -- bring up the "open dialog window"
end tell
delay aShortDelay
tell application "TextEdit" to activate
delay aShortDelay
tell application "System Events"
keystroke "g" using {shift down, command down} -- bring up the "goto folder sheet" in the open dialog window
delay aShortDelay
keystroke posixPath -- enter the posix file path
delay aShortDelay
keystroke return -- dismiss the "goto folder sheet"
delay aShortDelay
keystroke return -- dismiss the "open dialog window"
end tell
Notice I have delays after every command. That is good practice when you're using this type of scripting. You don't want the code to run faster than the computer can perform the tasks so the delay gives the computer time to keep up with the code. Notice that I use a variable for the delay. This way I can play with aShortDelay making it longer or shorter as needed to make the script run properly.
Good luck.
Aren’t you asking Isadora to import a list of folders? Perhaps it can only handle one folder at a time?
I don’t have Isadora to test, but you might try this:
on adding folder items to this_folder after receiving these_items
try
tell application "Isadora"
repeat with the_item in these_items
import media into document 1 from the_item
end repeat
end tell
on error msg
display dialog "Error importing file into Isadora: " & msg
end try
end adding folder items to

Applescript command that navigates to a specific folder when the attachments popup appears in safari?

I'm building an Applescript that will attach specific files when the attachment prompt appears in Safari.
I initially tried do shell script "open /Users/ea/Desktop/Guru/Deliverables/" but that just opens a new finder window. I need to know how to navigate to the correct folder once the prompt appears (see image below).
I'm sure it's simple, but I'm brand new to Applescript.
The GUI script solution would be:
tell application "System Events" to tell process "Safari"
tell window 1 to tell sheet 1
key code 5 using {shift down, command down} --shortcut for go to folder
tell sheet 1
set value of text field 1 to <POSIX path to targetfolder as text>
click button 1
end tell
end tell
end tell

Script for adding new KeyboardShortcuts on MacOs (Leopard)

Is it possible to add KeyboardShortcuts in MacOs (Leopard) using shell or other programmatic way? Basically, something to automate the steps of opening Keyboard&Mouse in SystemPreferences, selecting the last tab "KeyboardShortcuts", Clicking "+" to add a new one and filling the info.
Thank you
The following AppleScript should do the trick, with 3 variables:
app_name: name of an application that you want to assign the shortcut to, e.g. Safari
menu_title: exact menu name to execute
keystrokes: the actual shortcut
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell process "System Preferences"
tell window "Keyboard"
click button 3 of tab group 1
tell sheet 1
click pop up button 1
click last menu item of menu 1 of pop up button 1
keystroke "/Applications/" & app_name & ".app"
keystroke return
keystroke return
delay 1
keystroke menu_title
keystroke tab
keystroke last item of keystokes using rest of reverse of keystokes
delay 1
click button "Add"
end tell
end tell
end tell
end tell
The code is referencing the following site:
http://www.rngtng.com/2010/10/29/applescript-to-create-keyboard-shortcuts/

Applescript file dialog with UI scripting

I am trying to open a file in a not so scriptable area of an application.
I got halfway there by using UI scripting to select the proper menu item, but this opens a standard file dialog.
How can I set the destination of the file dialog with Applescript?
ahh ok this should get you going on the right path
tell application "Safari"
activate
-- do menu select gui script first
set posixpath to "/path/to/a/folder/that/exists"
tell window 1
tell application "System Events"
keystroke "g" using {shift down, command down}
keystroke posixpath
delay 1
keystroke return
end tell
end tell
end tell

Resources