2011-01-28 66 views
0

因此,我有一個應用程序'myApp',並且我有一個首選項來登錄時加載'myApp'。 我通過的launchd有這一切運行良好:啓動運行並隱藏應用程序

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
    <dict> 
    <key>Label</key> 
     <string>com.myAppDomain.myApp</string> 
    <key>ProgramArguments</key> 
     <array> 
     <string>/Applications/myApp.app/Contents/MacOS/myApp</string> 
     </array> 
    <key>RunAtLoad</key> 
     <true/> 
    </dict> 
</plist> 

我也想給用戶也隱藏選項「對myApp」

我試圖創建一個bash腳本,並增加了ProgramArguments陣列在我lauchd的plist:

#!/bin/sh 

osascript=/usr/bin/osascript 

$osascript -e 'tell application "System Events" to set visible of process "'myApp'" to false' 

exit 0 

但無論是運行失敗,或更可能我的應用程序運行之前有機會初始化。

有沒有更簡單的方法來做到這一點,我只是俯瞰? 在此先感謝。

回答

1

你可以通過調用

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HideOnLaunch"]; 

當用戶選擇隱藏您在啓動應用程序只需設置你的喜好的plist一個布爾值。

然後,當你的應用程序通過的launchd啓動,應用程序本身可以applicationDidFinishLaunching:檢查HideOnLaunch設置,並相應地隱藏自身:

if([[NSUserDefaults standardUserDefaults] boolForKey:@"HideOnLaunch"]){ 
    [[NSApplication sharedApplication] hide:nil]; 
} 

不要讓launchd隱藏你的應用程序!

另一種方法如下:您可以輕鬆地將參數傳遞給Cocoa程序。正如this NSUserDefaults document所描述的,如果你推出一個Cocoa程序是這樣的:

AnApp.app/Contents/MacOS/AnApp -FuBar YES 

然後你就可以通過[[NSUserDefaults standardUserDefaults] boolForKey:@"FuBar"]獲得價值YES

因此,根據用戶的喜好,你可以寫一個launchd的plist設定自變量-HideOnLaunch YES-HideOnLaunch NO

因此,在您的應用程序委託,想必在applicationDidFinishLaunching:,取決於是否在程序參數
HideOnLaunch設置隱藏自己的應用程序。

0

謝謝Yuji。

我結束了一個展開的plist這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
     <key>Label</key> 
    <string>com.myAppDomain.MyApp</string> 
    <key>ProgramArguments</key> 
    <array> 
     <string>/bin/sh</string> 
     <string>-c</string> 
     <string>/Applications/MyApp.app/Contents/MacOS/MyApp -hideOnLogin YES</string> 
    </array> 
    <key>RunAtLoad</key> 
    <true/> 
</dict> 
</plist> 

我加了bash腳本作爲ProgramArguments鑰匙串,蘋果確實在下面的plist:

~/Library/LaunchAgents/com.apple.FTMonitor.plist 

的hideOnLogin密鑰只能通過啓動的plist訪問,並在myApp退出時重置。我有一個綁定到另一個關鍵「hideOnLoad」複選框,當這個變化,我重寫推出的plist要麼:

/Applications/MyApp.app/Contents/MacOS/MyApp -hideOnLogin YES 

/Applications/MyApp.app/Contents/MacOS/MyApp 

視情況而定。

在啓動時,我檢查兩個默認值是否都爲真,如果是,我隱藏myApp,如下所示: [NSApp hide:self];

再次感謝您指點我正確的方向!

相關問題