• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How do we make hierarchial menu?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How do we make hierarchial menu?


  • Subject: Re: How do we make hierarchial menu?
  • From: Takaaki Naganoya <email@hidden>
  • Date: Thu, 15 Feb 2018 22:43:03 +0900

Thanks, Shane!
I modified your code and added more flexibility.


set aList to {"Piyomaru", "Software", "", "Takaaki", {"Yes", "No"}, "",
"Naganoya", {"MacBook Air", "Mac mini"}, "", "Quit"}


<AppleScript>



on createMenu(aList)
        set aMenu to current application's NSMenu's alloc()'s init()
        set aCount to 10

        set prevMenuItem to ""

        repeat with i in aList
                set j to contents of i
                set aClass to (class of j) as string

                if j is equal to "" then
                        set aMenuItem to (current application's NSMenuItem's
separatorItem())
                        (aMenu's addItem:aMenuItem)
                else
                        if (aClass = "text") or (aClass = "string") then

                                if j = "Quit" then
                                        set aMenuItem to (current application's
NSMenuItem's alloc()'s initWithTitle:j action:"actionHandler:" keyEquivalent:"")
                                else
                                        set aMenuItem to (current application's
NSMenuItem's alloc()'s initWithTitle:j action:"actionHandler:" keyEquivalent:"")
                                end if

                                (aMenuItem's setTag:aCount)
                                (aMenuItem's setTarget:me)
                                (aMenu's addItem:aMenuItem)

                                set aCount to aCount + 10
                                copy aMenuItem to prevMenuItem


                        else if aClass = "list" then
                                --Generate Submenu
                                set subMenu to current application's NSMenu's
new()
                                (aMenuItem's setSubmenu:subMenu)

                                set subCounter to 1

                                repeat with ii in j
                                        set jj to contents of ii

                                        set subMenuItem1 to (current
application's NSMenuItem's alloc()'s initWithTitle:jj action:"actionHandler:"
keyEquivalent:"")
                                        (subMenuItem1's setTarget:me)
                                        (subMenuItem1's setTag:(aCount +
subCounter))
                                        (subMenu's addItem:subMenuItem1)

                                        set subCounter to subCounter + 1
                                end repeat

                        end if

                end if

        end repeat

        return aMenu
end createMenu

</AppleScript>


> 2018/02/15 20:10、Shane Stanley <email@hidden>のメール:
>
> On 15 Feb 2018, at 7:42 pm, Takaaki Naganoya <email@hidden> wrote:
>>
>> How do we make hierarchical menu ?
>
> Here's your createMenu() handler from the page you linked to modified to add
> a submenu:
>
> on createMenu(aList)
>       set aMenu to current application's NSMenu's alloc()'s init()
>       set aCount to 1
>
>       repeat with i in aList
>               set j to contents of i
>
>               if j is not equal to "" then
>
>                       if j = "Quit" then
>                               set aMenuItem to (current application's
> NSMenuItem's alloc()'s initWithTitle:j action:"actionHandler:"
> keyEquivalent:"")
>                               --(aMenuItem's
> setKeyEquivalentModifierMask:(current application's NSControlKeyMask))
>                       else
>                               set aMenuItem to (current application's
> NSMenuItem's alloc()'s initWithTitle:j action:"actionHandler:"
> keyEquivalent:"")
>                       end if
>
>               else
>                       set aMenuItem to (current application's NSMenuItem's
> separatorItem())
>               end if
>               (aMenuItem's setTarget:me)
>               (aMenuItem's setTag:aCount)
>               (aMenu's addItem:aMenuItem)
>               if j is "Takaaki" then
>                       set newMenu to current application's NSMenu's new()
>                       (aMenuItem's setSubmenu:newMenu)
>                       set newMenuItem to (current application's NSMenuItem's
> alloc()'s initWithTitle:"Yes" action:"actionHandler:" keyEquivalent:"")
>                       (newMenuItem's setTarget:me)
>                       (newMenu's addItem:newMenuItem)
>                       set newMenuItem to (current application's NSMenuItem's
> alloc()'s initWithTitle:"No" action:"actionHandler:" keyEquivalent:"")
>                       (newMenuItem's setTarget:me)
>                       (newMenu's addItem:newMenuItem)
>               end if
>
>               if j is not equal to "" then
>                       set aCount to aCount + 1
>               end if
>       end repeat
>
>       return aMenu
> end createMenu
>
>
> --
> Shane Stanley <email@hidden>
> <www.macosxautomation.com/applescript/apps/>, <latenightsw.com>
>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> AppleScript-Users mailing list      (email@hidden)
> Help/Unsubscribe/Update your Subscription:
> Archives: http://lists.apple.com/archives/applescript-users
>
> This email sent to email@hidden

--
Takaaki Naganoya
email@hidden
http://piyocast.com/as/



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >How do we make hierarchial menu? (From: Takaaki Naganoya <email@hidden>)
 >Re: How do we make hierarchial menu? (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: How do we make hierarchial menu?
  • Next by Date: Question of technique: Speed
  • Previous by thread: Re: How do we make hierarchial menu?
  • Next by thread: Question of technique: Speed
  • Index(es):
    • Date
    • Thread