Script for adding new KeyboardShortcuts on MacOs (Leopard) - shell

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/

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 in automator error ("The action "Run AppleScript" encountered an error ")

I wanted to find a way how to disable/enable hot corners via keyboard shortcut.
I found this apple script
property theSavedValues : {"Mission Control", "Desktop", "Dashboard", "Launchpad"} -- for example
tell application "System Preferences"
set current pane to pane id "com.apple.preference.expose"
tell application "System Events"
tell window "Mission Control" of process "System Preferences"
click button "Hot Corners…"
tell sheet 1
tell group 1
set theCurrentValues to value of pop up buttons
if theCurrentValues is {"-", "-", "-", "-"} then
repeat with i from 1 to 4
set thisValue to item i of theSavedValues
tell pop up button i
click
click menu item thisValue of menu 1
end tell
end repeat
else
copy theCurrentValues to theSavedValues
repeat with i from 1 to 4
tell pop up button i
click
click last menu item of menu 1
end tell
end repeat
end if
end tell
click button "OK"
end tell
end tell
end tell
quit
end tell
So I created an service in Automator with this apple script, attached it to keyboard shortcut, whenever I run this script in automator, it works but when i hit the shortcut, im getting "The action "Run AppleScript" encountered an error ".
Any ideas what can be done to fix this?
Thank you
Oh my god I feel so dumb, the only thing needed was to grant accessibility permission to app from which I'm using this service :).
You can do this by going to (Mac OS)Settings -> Security&Privacy -> Accessibility -> and finding the app from which you are running the service. (In my case it's Finder.)

GUI automation with AppleScript - How to click a button in the script

I'm trying to write an AppleScript that will use the Save as Pictures... function of PowerPoint, but I'm wrestling with AppleScript. This is what I have:
set p to "file.pptx"
tell application "Microsoft PowerPoint"
launch
open p
delay 2
click menu item "Save as Pictures..." of menu bar item "File" of menu bar 1
end tell
and it isn't doing what I want. The specific error I get is:
script error: Expected end of line, etc. but found class name. (-2741)
And I don't know what to do. I've tried all sorts of things but I can't seem to get the right menu item to click. I'm using OSX 10.9 and PowerPoint 2011 (Version 14.3.2)
UPDATE:
This is now the script I have:
set p to "file.pptx"
tell application "Microsoft PowerPoint"
launch
open p
end tell
delay 2
tell application "System Events"
tell process "PowerPoint"
click menu item "Save as Pictures..." of menu 1 of menu bar item "File" of menu bar 1
end tell
end tell
And I'm getting an execution error: Microsoft PowerPoint got an error: Parameter error. (-50)
Gui automation is generally done through the "System Events" app.
Also you need to provide a full path to a file before trying to open it.
This is how you would begin your attack on PowerPoint correctly.
set p to POSIX file "/Users/drew/Desktop/file.pptx"
tell application "Microsoft PowerPoint"
launch
open p
end tell
delay 2
tell application "System Events"
tell process "PowerPoint"
click menu item "Save as Pictures..." of menu 1 of menu bar item "File" of menu bar 1
end tell
end tell

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.

AppleScript to save web content as PDF throws error : “Can't set window "Save" to …”

I am trying to save the web content of a URL I have opened in Safari as PDF. I was able to successfully achieve it through Automator but I want an AppleScript to do this.
Here is the script I am trying to run, which is throwing me an error:
error "System Events got an error: Can’t set window \"Save\" to \"~/Desktop/Reports/\"." number -10006 from window "Save"
Steps I did:
created a shortcut for Save as PDF…
opened Safari that has some web content
run the following script:
set destination_folder to "/Users/swatt/Desktop/Reports"
tell application "System Events"
keystroke "p" using {command down} -- activate print menu item
delay myDelay
keystroke "p" using {command down, option down, control down} -- to select the Save as PDF… option
keystroke "g" using {shift down, command down} -- to select the folder location
set value of text field 1 of sheet of window "Save" to destination_folder
-- Now click the Go button
click button "Go" of sheet of window "Save"
delay myDelay
-- Now that we are in our desired folder, set the file name and save
set value of text field 1 of window "Save" to "TestResults.pdf"
--click button "Save" of window "Save"
click button "Go"
end tell
Keyboard shortcuts work on the frontmost application, but for the UI elements (window, text field, ...), you must specify the process
tell application "System Events"
tell application process "Safari"
keystroke "p" using {command down} -- activate print menu item
-- other lines
--
end tell
end tell

Resources