Emulate SoftKeys with AppleScript shortcut

End user supplied how to documents and discussion.

Emulate SoftKeys with AppleScript shortcut

Postby Critalsh » Fri Feb 21, 2014 10:17 am

As have others, I have been frustrated by the failure of SoftKeys under 10.9 (and 10.7 & 10.8 I think...) Anyway, should others find this helpful, I have modified an AppleScript to achieve the same end. I had a soft key shortcut that let me choose which of several common applications I wanted to open (rather than have to remember a different trigger key for each...), and some often used QK shortcuts. This workaround example does the same. It can directly launch applications, or QuicKeys Shortcuts (in any combination). The main purpose of this AppleScript it to replicate the functionality of the now non-functional soft key feature of QK.

You will need a QuicKeys shortcut with only one step which launches the workaround AppleScript. When this QuicKeys shortcut is run, an AppleScript dialogue appears. The user makes a selection and then presses OK to proceed. Carefully choosing different initial letters of the items to select means they can be chosen simply by pressing this initial letter instead of having to use the mouse pointer. In the example below therefore, run the QuicKeys shortcut (the one which starts the AppleScript), then press P {for Pages} then OK. Bingo: Pages opens! (My example works because I have the indicated applications or QK "Hello World..." shortcut on my computer.)


Notes:
  1. Application Name or File name MUST be unique, so I changed "Microsoft Excel" to "Microsoft Excel 2008" because I also have "Microsoft Excel" from 2004 which had the same name. This is an aspect of AppleScript. (If you don't want to change a file's name to make it you, it seems like there is a way to give AppleScript the complete path to the file, by I couldn't make that to work...)
  2. Note that -as an example- a QK script call is included; I have put a "1" in from of it so that --should the user wish to have several-- they can be selected by number...

Here is my script, which others are welcome to use and modify as they need. Important: Use at your own risk of course.

Code: Select all
--Set Groups
-- Modify this list to suit your requirements; it can be longer or shorter;
-- (Lines preceded by '--' are comments and do not execute
-- Note difference between double and single quotes...
--
set appPages to {"Pages"}
set appTextEdit to {"TextEdit"}
set appNumbers to {"Numbers"}
set appExcel2008 to {"Microsoft Excel 2008"}
set appQuickeys to {"QuicKeys"}
-- 
set appGroups to {"Pages", "TextEdit", "Numbers", "Microsoft Excel", "1 QK 'Hello World...'"}

--App Launcher Handler
on appLauncher(selectedGroup)
   repeat with i from 1 to (count of items in selectedGroup)
      tell application (item i of selectedGroup)
         activate
      end tell
   end repeat
end appLauncher

--Let the user select a group
set theSelection to choose from list appGroups with title "What next?"

--If Selection cascade (should correspond to the number of set groups above)
if theSelection is false then
   error number -128
else
   set theSelection to item 1 of theSelection
   
   --If selection is appPages
   if theSelection = (item 1 of appGroups) then
      appLauncher(appPages)
      
      --If selection is appTextEdit
   else if theSelection = (item 2 of appGroups) then
      appLauncher(appTextEdit)
      
      --If selection is appNumbers
   else if theSelection = (item 3 of appGroups) then
      appLauncher(appNumbers)
      
      -- USE THE NEXT FOUR LINES {from 'If selection...'} AS A TEMPLATE SO THAT EACH ITEM IN YOUR
      -- LIST OF OPTIONS HAS ITS CORRESPONDING OUTCOME
      --If selection is appExcel2008
   else if theSelection = (item 4 of appGroups) then
      appLauncher(appExcel2008)
      
      --If selection is appQuickeys
   else if theSelection = (item 5 of appGroups) then
      tell application "QuicKeys"
         play shortcut named "Hello World Message called from AppleScript" -- Substitute here the name of an existing shortcut on your machine.
      end tell
      
   else
      -- this handles the selection of the choice of 'Cancel'
      return
   end if
end if


PS: I am not an AppleScript expert, so I can't really help if this AppleScript doesn't work for you. Sorry.
Enjoy!
Critalsh
 
Posts: 12
Joined: Tue Mar 25, 2008 12:57 pm

Re: Emulate SoftKeys with AppleScript shortcut

Postby Critalsh » Thu Feb 27, 2014 11:35 am

Update on 27 Feb 2014 & 17 March 2014 of AppleScript code to make selected application become the frontmost application, except for Excel and Word. Made list of applications shorter...

Code: Select all
-- Set Groups
-- Modify this list to suit your requirements; it can be longer or shorter;
-- (Lines preceeded by '--' are comments and do not execute)
-- (Note difference between double and single quotes...)
-- Modified 16-March-2014 to shorter list, and special handling for Microsoft Excel & Word
--
set appParallels to {"Parallels Desktop"} -- Menu selection 1
set appPages to {"Pages"} -- Menu selection 2
set appTextEdit to {"TextEdit"} -- Menu selection 3
set appNumbers to {"Numbers"} -- Menu selection 4
set appExcel2008 to {"Microsoft Excel 2008"} -- Menu selection 5
set appMail to {"Mail"} -- Menu selection 6
set appWord2008 to {"Microsoft Word 2008"} -- Menu selection 7
-- 
set appGroups to {"1 Parallels Desktop", "2 Pages", "3 TextEdit", "4 Numbers", "5 Microsoft Excel", "6 Mail", "7 Microsoft Word"}

--App Launcher Handler
on appLauncher(selectedGroup)
   repeat with i from 1 to (count of items in selectedGroup)
      tell application (item i of selectedGroup)
         activate
      end tell
      tell application "System Events" to set frontmost of process (item i of selectedGroup) to true
   end repeat
end appLauncher

on appLauncherMicrosoftApps(selectedGroup)
   repeat with i from 1 to (count of items in selectedGroup)
      tell application (item i of selectedGroup)
         activate
      end tell
   end repeat
end appLauncherMicrosoftApps

-- Let the user select a group
set theSelection to choose from list appGroups with title "What next?"

--If Selection cascade (should correspond to the number of set groups above)
if theSelection is false then
   error number -128
else
   set theSelection to item 1 of theSelection
   
   --If selection is appParallels
   if theSelection = (item 1 of appGroups) then
      -- tell application "QuicKeys"
      -- play shortcut named "Open Snow Leopard in Parallels AS" -- Substitute here the name of an existing shortcut on your machine.
      -- end tell
      appLauncher(appParallels)
      
      --If selection is appPages
   else if theSelection = (item 2 of appGroups) then
      appLauncher(appPages)
      
      --If selection is appTextEdit
   else if theSelection = (item 3 of appGroups) then
      appLauncher(appTextEdit)
      
      --If selection is appNumbers
   else if theSelection = (item 4 of appGroups) then
      appLauncher(appNumbers)
      
      --If selection is appExcel2008
   else if theSelection = (item 5 of appGroups) then
      appLauncherMicrosoftApps(appExcel2008)
      
      --If selection is appMail
   else if theSelection = (item 6 of appGroups) then
      appLauncher(appMail)
      
      --If selection is appWord2008
   else if theSelection = (item 7 of appGroups) then
      appLauncherMicrosoftApps(appWord2008)
      
   else
      -- this handles the selection of the choice of 'Cancel'
      return
   end if
end if
Critalsh
 
Posts: 12
Joined: Tue Mar 25, 2008 12:57 pm


Return to QKM How Tos

Who is online

Users browsing this forum: No registered users and 4 guests

cron