2011-11-04 100 views
3

是否可以檢查任何應用程序是否打開文件或包?例如,假設我知道/Users/Foo/AwesomeDocument.txt存在並且它在TextEdit中打開,我可以從我的應用程序可靠檢查文檔是否打開? 我很滿意那些只能通過NSDocumentController打開文檔的解決方案。檢查文件是否在任何應用程序中打開

+0

哦,來吧,我不想爲我創建的每個問題開始獎勵... – JustSid

+0

你是否認爲你可能會問一個難題? –

+0

@RobKeniger是的,這就是爲什麼我在這裏問。 – JustSid

回答

1

您可以使用NSTask運行shell命令lsof | grep "Document.txt",然後解析結果,儘管該方法有點慢。我不知道一個本地的可可方式來實現這一點。

+0

不幸的是,這不適用於通過'NSDocumentController'打開的文件,因爲它沒有整個文件句柄。 – JustSid

0

你的意思是這樣嗎?

例如,在AppleScript的:

tell application "TextEdit" 
    set theDocument to document of window 1 
    return theDocument 
end tell 
+0

不完全是,我想檢查一個已知的文件是否打開,而不是當前在TextEdit中打開的文件(因此反向版本會很棒)。它也應該跨空間工作=/ – JustSid

0
tell application "TextEdit" 
    set theDocuments to every document 
    repeat with aDocument in theDocuments 
     if (path of aDocument) as string is equal to "/test.txt" then 
      display dialog "found" 
     end if 
    end repeat 
end tell 
+0

對不起,但我仍然需要它。詢問每一個應用程序,如果文檔是打開的是一些開銷,我真的不想要,我寧願只使用路徑,並知道文件是否在某處打開。 – JustSid

0

從10.7開始,你可能會想嘗試一些與NSFileCoordinator。請記住,並不是每個應用都必須實現NSFilePresenter,並與NSDocument一起前進。