Select menu item from context menu?

The object is to save a bookmark to a particular bookmarks folder without using the mouse or keyboard.

So far I've got the context menu to appear, but I don't know how to select the proper menu item.
I've tried using the Select or Show a Menu Item. Do I have to enumerate the menu items?

I assume you mean after the macro is triggered.

I have a macro/script that does a similar thing: It creates a Chrome bookmark in a user-selected sub-folder of the Chrome Bookmark folder. The user selection is done in a KM Prompt for User input, and then passes that to the below script.

Chrome Bookmarks are very scriptable. Maybe the below script will help you get started:

property ptyScriptName : "Create BookMark in Browser Folder"
property ptyScriptVer : "2.1.1"
property ptyScriptDate : "2017-08-20"
property ptyScriptAuthor : "JMichaelTX"

property LF : linefeed

(*

  TODO:
    1. Test chooseBMFolder folder list for no sub-folders
    2. FIX folderPath in chooseBMFolder
    
  
  Local:  /Users/Shared/Dropbox/SW/DEV/KM/Scripts/@Web @Chrome Create @Bookmark in Bookmark @Folder @AS.scpt
*)

use AppleScript version "2.5" -- macOS 10.11.6+
use framework "Foundation"
use scripting additions

### REQUIRES ###
use KMLib : script "[LIB] JMichaelTX KM Lib AS"

------------------------------------
--  GET USER INPUT DATA FROM KM --
------------------------------------

tell application "Keyboard Maestro Engine"
  set bmFolderPath to getvariable "SCPT__BMFolderPath" -- POSIX Path of SubFolder
  set addDateOpt to getvariable "SCPT__AddDate" -- "PREFIX" or "SUFFIX" or "NONE"
  set bmTitle to getvariable "SCPT__BookMarkTitle"
  set pageURL to getvariable "SCPT__PageURL"
end tell


(*
The variable bmFolderPath must be in this form:
  SubFolder1/SubFolder1-1/SubFolder1-1-1
    where "SubFolder1" is a subfolder of "Bookmarks Bar"
    
  A "?" at the end of the folder path will cause a Prompt to Choose from the
  list of subfolders at that level.  For example:
  SubFolder1/SubFolder1-1/?
*)


--- CONVERT BM FOLDER PATH to LIST ---
set bmFolderList to splitStrToList(bmFolderPath, "/") of KMLib

set dateISOStr to text 1 thru 10 of ((current date) as «class isot» as string)

tell application "System Events"
  set appFrontMost to item 1 of (get name of processes whose frontmost is true)
end tell


if appFrontMost = "Safari" then
  ---------------------------------
  tell application "Safari"
    -------------------------------
    --- INSERT SAFARI CODE HERE ---
    
  end tell -- "Safari"
  
  set scriptResults to ("Sorry, " & ptyScriptName & " is NOT yet setup for Safari.")
  display dialog scriptResults with icon stop
  error scriptResults
  
  --- USE BELOW AFTER Code is Added for SAFARI ---
  set scriptResults to "OK  " & ¬
    "Safari BookMark was CREATED in Folder: " & LF & bmFolderTitle
  
  
else if appFrontMost = "Google Chrome" then
  ------------------------------------
  tell application "Google Chrome"
    ----------------------------------
    
    if (addDateOpt = "PREFIX") then
      set bmTitle to "[" & dateISOStr & "] " & bmTitle
    else if (addDateOpt = "SUFFIX") then
      set bmTitle to bmTitle & " [" & dateISOStr & "]"
    end if
    
    set oBMFolder to bookmark folder "Bookmarks Bar"
    
    --set oBMFolder to bookmark folder (item 1 of bmFolderList) of bookmark folder "Bookmarks Bar"
    set chooseBMBol to false
    
    repeat with aBMTitle in bmFolderList
      set aBMTitle to contents of aBMTitle
      
      if (aBMTitle = "?") then
        if (length of bmFolderPath > 1) then
          set bmFolderPath to (text 1 thru -2 of bmFolderPath)
        else
          set bmFolderPath to ""
        end if
        set oBMFolder to my chooseBMFolder(oBMFolder, bmFolderPath)
        if (oBMFolder ≠ "") then
          set bmFolderPath to bmFolderPath & "/" & (title of oBMFolder)
        end if
        exit repeat
      end if
      
      set oBMFolder to bookmark folder (aBMTitle) of oBMFolder
    end repeat
    
    if (oBMFolder ≠ "") then
      tell oBMFolder
        set newBM to make new bookmark item with properties {title:bmTitle, URL:pageURL}
      end tell
      set scriptResults to "OK  " & ¬
        "Chrome BookMark was CREATED in Folder: " & LF & bmFolderPath
    else
      set scriptResults to "[USER CANCELED]" & LF & "A BookMark was NOT Created."
    end if
    
  end tell -- "Google Chrome"
  
  
else
  error ("ERROR:  Invalid Broswer Name: " & appFrontMost ¬
    & return & "MUST BE either \"Safari\" OR \"Chrome\"")
  
end if -- Switch Based on appFrontMost

------------------------------------------------------------------

set msgStr to scriptResults
set msgTitleStr to ptyScriptName
display notification msgStr with title msgTitleStr sound name "Tink.aiff"

return scriptResults

--~~~~~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~


on chooseBMFolder(pParentFolder, pFolderPath)
  local folderList, oBMFolder, bmFolderTitle
  
  tell application "Google Chrome"
    
    set oBMFolder to pParentFolder
    set folderList to title of every bookmark folder of oBMFolder
    set folderList to {"[SHOW SUB-FOLDERS FOR:]"} & folderList
    
    set showSFBool to true
    repeat while showSFBool
      set bmFolderTitle to choose from list folderList ¬
        with title ptyScriptName ¬
        with prompt "PARENT Folder: " & pFolderPath & LF & LF & ¬
        "Select Folder for Your BookMark" & LF & LF & ¬
        "OR" & LF & LF & ¬
        "Select" & LF & "[SHOW SUB-FOLDERS FOR:]" & LF & ¬
        "AND the Folder you want to see sub-folders for." with multiple selections allowed
      
      if ((bmFolderTitle is false) or ((count of bmFolderTitle) = 1)) then exit repeat
      
      set bmFolderTitle to contents of item 2 of bmFolderTitle
      set pFolderPath to pFolderPath & "/" & bmFolderTitle
      set oBMFolder to bookmark folder bmFolderTitle of oBMFolder
      
      set folderList to title of every bookmark folder of oBMFolder
      set folderList to {"[SHOW SUB-FOLDERS FOR:]"} & folderList
      
      
    end repeat
    
    if (bmFolderTitle is false) then
      set oBMFolder to ""
    else
      set bmFolderTitle to item 1 of bmFolderTitle
      set oBMFolder to bookmark folder bmFolderTitle of oBMFolder
    end if
    
  end tell
  
  return oBMFolder
  
end chooseBMFolder

I assume the meaning of your post is that a script is needed to accomplish what I want.

Hey Dave,

That action is for working with menus in the menu bar.

Keyboard Maestro has no direct ability to select items in pop-up menus.

In many apps you can use type-select with an Insert Text by Typing action to get where you want, but Firefox (and some other apps) does NOT respond normally to type-ahead in the contextual menu.

JM's scripted method WON'T work for you, because Firefox uses unconventional coding methods that AppleScript UI-Scripting cannot see.

You're only real choice here is to use the Click at Found Image action to select your first level contextual menu item and then go on from there.

By the way – you really need to tell us what app you're working in, so we don't have to guess.

How Do I Get The Best Answer in the Shortest Time?

-Chris

Yes, if you are running Safari or Chrome. FireFox is NOT scriptable.

However, there many be FF extensions that will provide this function.

I have a small number of folders to which I regularly save bookmarks in Safari. One hot key activates a Keyboard Maestro palette, and a second key press selects the desired bookmarks folder.

As built, this macro is dependent on the Type A Keystroke action to navigate the dialog that Safari presents the user. I don't know if something similar could be achieved in Chrome or Firefox since I'm not familiar with the interface they present users.

Just type the name of the menu item to select it. So, use a "Insert Text by Typing" in Keyboard Maestro.

1 Like

This won't work in Firefox...

-Chris