Xcode:我如何为这个cocoscript应用程序制作一个切换按钮?

时间:2013-02-08 15:59:09

标签: xcode button applescript

AppDelegate.applescript

--  iTunes Switcher
--
--  Created by admini on 11/13/12.
--  Copyright (c) 2012 Bdaniels. No rights reserved.
--  



script AppDelegate
    property parent : class "NSObject"

    on ButtonHandlerActivationOn_(sender)
        tell application "iTunes" to quit
        do shell script "/usr/bin/defaults write com.apple.iTunes StoreActivationMode -integer 1"
        delay 1
        do shell script "open -a itunes"
    end ButtonHandlerActivationOn

    on ButtonHandlerActivationOff_(sender)
        tell application "iTunes" to quit
        do shell script "/usr/bin/defaults write com.apple.iTunes StoreActivationMode -integer 0"
        delay 1
        do shell script "open -a itunes"
    end ButtonHandlerActivationOff




    on applicationWillFinishLaunching_(aNotification)
        -- Insert code here to initialize your application before any files are opened 
    end applicationWillFinishLaunching_

    on applicationShouldTerminate_(sender)
        -- Insert code here to do any housekeeping before your application quits 
        return current application's NSTerminateNow
    end applicationShouldTerminate_

end script  

以下是UI的屏幕截图 http://imgur.com/8xO9K4c

如果打开状态,我想点开绿灯。 任何有关这方面的帮助都会很棒!这里还有两个按钮,但是我省略了它们,因为我的帖子中有“Too much code”,stackoverflow不允许我发布它。

提前致谢!

1 个答案:

答案 0 :(得分:0)

首先你需要一个图像,一个绿色图像。将其添加到您的项目中。在代码中我使用“green.png”作为其名称。然后添加这些以便您可以访问NSImage方法并将图像添加到按钮。

property NSImage : class "NSImage"
property greenLightButton : missing value
property greenLightImage : missing value

接下来在窗口中添加“方形”按钮。这将显示您的图像。使其成为绿色图像所需的尺寸。在属性检查器中,取消选中按钮的“有边框”。在这一点上它基本上看起来不可见。将greenLightButton属性连接到该按钮。接下来添加此代码。

on applicationWillFinishLaunching_(aNotification)
   set greenLightImage to NSImage's imageNamed_("green.png")
end applicationWillFinishLaunching_

on isActivationOn()
   set isOn to false
   try
      do shell script "/usr/bin/defaults read com.apple.iTunes StoreActivationMode"
      if result is "1" then set isOn to true
   end try
   return isOn
end isActivationOn

然后你的代码中的任何地方,可能在你的按钮方法和applicationWillFinishLaunching中,你想要这样的东西......

if (isActivationOn()) then
   greenLightButton's setImage_(greenLightImage)
else
   greenLightButton's setImage_(missing value)
end