Applescript: how to click on checkbox only if is not already selected? - applescript

I want to enable option "Open in Low Resolution" for all ".app" files in the system.
This is my Applescript code:
on run argv
tell application "Finder"
set myFile to (POSIX file (item 1 of argv)) as alias
open information window of myFile
activate
tell application "System Events"
try
click checkbox "Open in Low Resolution" of scroll area 1 of (windows where name contains "Info") of process "Finder"
end try
end tell
close information window of myFile
end tell
end run
i run it with:
sudo find / -name *.app -type d -exec osascript myScript.scpt "{}" \;
This work very good: it looks for all ".app" files, pass the file to the script, the script will open the "information window" of the file, clicks the "Open in Low Resolution" checkbox and finally it closes the window.
Problem: i need to click on the Checkbox ONLY if is not already selected.
I tried with this solution (and other similar):
Tick a checkbox only if it's not selected
Nothing happens. No errors.
System Events, Applescripts ecc... are allowed to run on Security And Privacy Settings
This does not works:
on run argv
tell application "Finder"
--set myFile to (POSIX file (item 1 of argv)) as alias
set myFile to (POSIX file ("/Applications/App Store.app")) as alias
open information window of myFile
activate
end tell
tell application "System Events" to tell process "Finder"
try
set theCheckbox to checkbox "Open in Low Resolution" of scroll area 1 of (windows where name contains "Info") of process "Finder"
tell theCheckbox
set checkboxStatus to value of theCheckbox as boolean
if checkboxStatus is false then click theCheckbox
end tell
end try
end tell
tell application "Finder"
delay 1
close information window of myFile
end tell
end run
It simply does nothing.
The following sample (with an app Info Window opened):
tell application "System Events" to tell process "Finder"
set theCheckbox to checkbox "Open in Low Resolution" of scroll area 1 of (windows where name contains "Info")
tell theCheckbox
if not (its value as boolean) then click theCheckbox
end tell
end tell
Will produce the following error:
Can’t make «class valL» of {«class chbx» "Open in Low Resolution" of
«class scra» 1 of window "App Store.app Info" of «class pcap» "Finder"
of application "System Events"} into type boolean
What can i do next?
Best regards.

I suspect the problem is that you've put everything inside a Finder tell block. The Finder is notoriously finicky about scripting; best to use it as little as possible.
Rearranging things to use System Events seems to fix the problem (though admittedly my old MacBook Pro doesn't have a retina screen, so I had to test this using a different checkbox). Notice that you don't need to activate the Finder or the information window; you can do this all in the background.
on run argv
set myAppPath to item 1 of argv -- should be a POSIX path
tell application "Finder"
open information window of file (POSIX file myAppPath as text)
end tell
tell application "System Events"
set appName to displayed name of disk item myAppPath
tell process "Finder"
tell window (appName & " info")
tell first scroll area
if (value of checkbox "Open in Low Resolution" as boolean) is false then
click checkbox "Open in Low Resolution"
end if
end tell
click (first button whose role description is "close button")
end tell
end tell
end tell
end run

Related

Applescript: Open preview "Export to pdf" error: Preview got an error: Can’t get keystroke " "

I would like to open all the pdfs in a folder with preview and export them to another folder using "Export to PDF".
This is my issue so far.
Error
Preview got an error: Can’t get keystroke "
".
Apple Script
tell application "Finder"
set fl to files of folder POSIX file "/Users/documents/PDFs" as alias list
end tell
repeat with f in fl
tell application "Preview"
open f
tell application "System Events" to tell process "Preview"
delay 0.2
click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1
end tell
keystroke return
end tell
end repeat
You are getting that error because keystroke return is a system events command and you have it within the "Preview tell block"
It looks as if your intention of keystroke return is to actually click the "Save" button, then this should work for you...
tell application "Preview"
activate
tell application "System Events" to tell process "Preview"
delay 0.2
click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1
delay 0.2
click button "Save" of sheet 1 of window 1
end tell
end tell

Applescript - won't work when used as a droplet

I'm trying to create a script to use as a droplet where I drop a file onto it, automator opens it in Adobe Acrobat, then applescript runs an action inside of Acrobat to save it as an optimized PDF into a specific folder, then opens that new file in preview and uses a quartz filter to reduce the size of the PDF.
If I have a file already open in Acrobat and just run the script, it executes the action within Acrobat, saves the file, then opens preview (haven't gotten any further). But if I drop the file onto the droplet it just gives me an error message.
Here's the code:
on run {input, parameters}
tell application "System Events"
tell application process "Acrobat"
tell application "Adobe Acrobat Pro" to activate
-- runs the custom action in Acrobat
click the menu item "SAVE in 0-SEND" of menu 1 of menu item "Action Wizard" of the menu "File" of menu bar 1
end tell
end tell
tell application "System Events"
keystroke return
-- clears the action finished message
end tell
tell application "Adobe Acrobat Pro"
activate
close documents saving no
quit
end tell
-- this doesn't work yet
tell application "Finder"
tell application "Preview"
open input
end tell
end tell
end run
I would recommend something along these lines...
on run
set aFile to choose file with prompt "Please select the file to process"
processPDF(aFile)
-- cleanup, quit the application
tell application "Adobe Acrobat Pro" to quit
end run
on open (theFiles)
repeat with aFile in theFiles
processPDF(aFile)
end repeat
-- cleanup, quit the application
tell application "Adobe Acrobat Pro" to quit
end open
on processPDF(theFile)
-- open your file
-- do something with theFile here.
-- close the file
end processPDF

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 click 'share dropbox link' using contextual menu

I am trying to access the 'share dropbox link' item the contextual menu of a specific file in a specific dropbox folder using AXShowMenu.
I have found the following script. Via Applescript right click a file
tell application "System Events"
tell process "Finder"
set target_index to 1
set target to image target_index of group 1 of scroll area 1
tell target to perform action "AXShowMenu"
end tell
end tell
Which seem to do the trick for desktop items,
but how can I use this to target specific files in folders then click the 'share dropbox link' in the contextual menu?
A little kludgey, but it does work. This requires you have the dropbox folder open and frontmost in Finder.
tell application "Finder"
activate --brings Finder to front
select item 1 of window 1 --selects the first item in the list ; not sure how you want to select/determine the item
end tell
delay 0.5 --this is to insure you don't get unwanted typing in script editor (makes sure Finder activates before kludgey typing gets done)
tell application "System Events"
tell application process "Finder"
set _selection to value of attribute "AXFocusedUIElement" --focused element (selected element)
tell _selection to perform action "AXShowMenu" --contextual menu
end tell
keystroke "share dropbox link" --type to select contextual menu item
keystroke return --select it by hitting return
end tell
[EDIT] Here's a trick to make sure your dropbox folder is open and frontmost in the Finder:
tell application "Finder"
activate --brings Finder to front
tell me to set dbFolder to POSIX file ((do shell script "cd ~/Dropbox/;pwd") & "/")
open dbFolder
select item 1 of window 1 --selects the first item in the list ; not sure how you want to select/determine the item
end tell
tell application "System Events"
tell application process "Finder"
set _selection to value of attribute "AXFocusedUIElement" --focused element (selected element)
tell _selection to perform action "AXShowMenu" --contextual menu
end tell
keystroke "share dropbox link" --type to select contextual menu item
keystroke return --select it by hitting return
end tell
There may be another way of selecting the contextual menu, but this is what I came up with so far late at night after a glass of wine.

AppleScript Clicking On dialog box

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.

Resources