使用Applescript创建一个简单的菜单栏应用程序

时间:2015-03-20 14:08:08

标签: objective-c macos applescript menubar applescript-objc

我正在尝试在OS X Yosemite中创建一个菜单栏应用程序,它只包含一个带子菜单的下拉菜单。此菜单及其子菜单将由applescript脚本填充。

我之前已经找到了类似的教程,但它们似乎都已经过时,并且在Xcode 6.2中没有正常工作,例如:

MenuApp_ASOC

我在Applescript方面经验丰富,但没有太多时间在Objective C中编写代码。

在哪里可以找到我想要创建的各种模板的好地方?

2 个答案:

答案 0 :(得分:5)

这是动态创建菜单系统的快速示例。这就是我认为你的目标。

在此示例中,每次单击“状态栏”菜单时,菜单项都将不同。

(约塞米蒂要求)

将此代码粘贴到新的脚本编辑器Applescript文档中。

使用另存为...菜单选项将其另存为保持打开的应用程序。

然后将应用程序作为普通应用程序运行。

    use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"

property StatusItem : missing value
property selectedMenu : "" -- each menu action will set this to a number, this will determin which IP is shown

property theDisplay : ""
property defaults : class "NSUserDefaults"
property internalMenuItem : class "NSMenuItem"
property externalMenuItem : class "NSMenuItem"
property newMenu : class "NSMenu"

property theList : "Jackson Aiden Liam Lucas Noah Mason Ethan Caden Jacob Logan Jayden Elijah Jack Luke Michael Benjamin Alexander "
-- example list for the menu items that can be used. Ideally you will have your list created dynamically 


-- check we are running in foreground - YOU MUST RUN AS APPLICATION. to be thread safe and not crash
if not (current application's NSThread's isMainThread()) as boolean then
    display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
    error number -128
end if

on menuNeedsUpdate:(menu)
    (* NSMenu's delegates method, when the menu is clicked this is called.

    We use it here to call the method makeMenus(). Which removes the old menuItems and builds new ones.

    This means the menu items can be changed dynamically.

    *)

    my makeMenus()
end menuNeedsUpdate:

on makeMenus()

    newMenu's removeAllItems() -- remove existing menu items

    -----< (* this is just to show in this example a dynamic list for the menu items
    set someListInstances to {}
    set counter to count of word of theList
    repeat until (count of someListInstances) is (random number from 3 to counter)

        set rnd to random number from 1 to counter
        set thisItem to word rnd of theList
        if thisItem is not in someListInstances then
            copy thisItem to end of someListInstances
        end if
    end repeat
    ----  <

    repeat with i from 1 to number of items in someListInstances
        set this_item to item i of someListInstances
        set thisMenuItem to (current application's NSMenuItem's alloc()'s initWithTitle:this_item action:"someAction:" keyEquivalent:"")

        (newMenu's addItem:thisMenuItem)

        (thisMenuItem's setTarget:me) -- required for enabling the menu item
        if i is equal to 3 then
            (newMenu's addItem:(current application's NSMenuItem's separatorItem)) -- add a seperator
        end if
    end repeat

end makeMenus



--menuItems  action is requied for the menu to be enabled
on someAction:sender
    --MenuItem --do some thing 
end someAction:

-- create an NSStatusBar
on makeStatusBar()
    set bar to current application's NSStatusBar's systemStatusBar

    set StatusItem to bar's statusItemWithLength:-1.0

    -- set up the initial NSStatusBars title
    StatusItem's setTitle:"IP"
    -- set up the initial NSMenu of the statusbar
    set newMenu to current application's NSMenu's alloc()'s initWithTitle:"Custom"

    newMenu's setDelegate:me (*
    Requied delegation for when the Status bar Menu is clicked  the menu will use the delegates method (menuNeedsUpdate:(menu)) to run dynamically update.


    *)

    StatusItem's setMenu:newMenu

end makeStatusBar


my makeStatusBar()

答案 1 :(得分:0)

我鼓励您访问ASOC论坛http://macscripters.net,那里有人可以回答您的问题,关于如何使用XCode 6.2。但是你必须努力记录你当然得到的问题/错误,简单地说#34;不工作&#34;为了解决你的问题,还不够好。

这是你必须要做的事情,除非你很高兴你找到一个适用于XCode项目的菜单应用程序。

在macscripters内​​部,http://macscripter.net/viewtopic.php?id=38891&p=1有一个由DJ Bazzie Wazzie制作的小型菜单应用程序项目,可能更容易上班。您可以解决该线程或ASOC论坛中的任何问题。

Shane Stanley,两本好的ASOC书籍的作者,以及ASOC编辑经常访问那里的论坛,是一个非常有帮助和善良的人!