2013-02-08 40 views
0

AppDelegate.applescriptXcode:我將如何爲此cocoscript應用程序製作切換按鈕?

-- 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

我想提出一個綠燈,如果狀態變成一個屏幕截圖。 任何幫助,這將是偉大的!這裏還有兩個按鈕,但我省略了它們,因爲我的帖子中有「代碼太多」,並且stackoverflow不允許我發佈它。

在此先感謝!

回答

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 
相關問題