簡體   English   中英

在Finder窗口OSX中打開文件

[英]Open file in Finder window OSX

我的問題是打開Finder窗口來顯示文件。

要打開一個文件,可以這樣做:

[[NSWorkspace sharedWorkspace] selectFile:file.path 
                 inFileViewerRootedAtPath:file.path];

現在,如果用戶試圖打開相同的file.path我需要一個Finder。

為此,我在數組中添加了file.path

但是當用戶關閉Finder窗口並再次嘗試打開文件時,我陷入困境(因為活動窗口具有帶路徑的數組)。 然后Finder沒有顯示:(

任何幫助如何繼續這一點。


  1. 有沒有辦法檢查Finder窗口打開的路徑?

  2. 有沒有辦法獲取finder的回調,以便我可以從數組中刪除file.path

這是通過簡單地傳遞nil來完成的。

[[NSWorkspace sharedWorkspace] selectFile:file.path inFileViewerRootedAtPath:nil];

現在,即使多次點擊,也會選擇相同的文件/文件夾。

我的第一選擇是在我的應用程序中創建像窗口一樣的查找器,並在其中打開文件,這將使您完全控制您的操作。

第二個選項可能是創建腳本化的可可應用程序。 這可以提供您在問題中描述的功能,也可以與沙盒兼容。 來自Apple: Gatekeeper和Signing Applets

上面的鏈接摘要。

Gatekeeper和簽名小程序OS X Mountain Lion包括Gatekeeper,它通過應用允許運行下載軟件的策略來保護用戶免受惡意軟件的攻擊。 Gatekeeper依賴於代碼簽名來驗證應用程序:簽名的應用程序保證由簽名者創建,並且自簽名以來未被修改。 默認情況下,Gatekeeper將僅允許運行已由Mac App Store或已識別的開發人員簽名的應用程序。 如果您編寫用於分發的腳本應用程序(“applet”),則此策略將應用於您的applet。 要簽署您的applet,以便Gatekeeper的默認策略不會阻止它們:

我們需要的:

  1. 創建* .sdef文件,該文件定義應用程序的腳本信息。 可編寫性信息
  2. 將Cocoa腳本支持添加到您的應用程序Cocoa支持可編寫腳本的應用程序
  3. 創建Applescript以打開“命名”Finder窗口並將信息發送回您的Cocoa應用程序,反之亦然。
  4. 從您的應用程序調用Applescript,反之亦然。

您需要創建* .sdef文件,這是一種基於XML的格式,它描述了一組腳本性術語以及描述應用程序可編寫腳本性的命令,類,常量和其他信息。 您需要將此文件添加到項目中。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">
<dictionary title="YOUR_APP_NAME">
    <suite name="scriptTest Suite" code="MApN" description="YOUR_APP_NAME Scripts">
        <command name="myFirstCommand" code="lkpstrng" description="The array to lookup">
            <cocoa class="MyLookupCommand"/> 
            <direct-parameter description="The array to lookup">
                <type type="any" list="yes"/>
            </direct-parameter>
            <result description="returns and array" type="text"/>
        </command>
    </suite>
</dictionary>

將* .sdef文件包含到項目中后,您需要向info.plist添加兩個新密鑰。 Scriptable = YES和腳本定義文件名。

在此輸入圖像描述

在定義為MyLookupCommand腳本定義文件cocoa class ,我們需要在Cocoa應用程序中創建此類。 MyLookupCommand類它是NSScriptCommand子類

。H

#import <Foundation/Foundation.h>
@interface MyLookupCommand : NSScriptCommand
@end

.M

#import "MyLookupCommand.h"

@implementation MyLookupCommand

-(id)performDefaultImplementation {

    // get the arguments
    NSDictionary *args = [self evaluatedArguments];
    NSString *stringToSearch = @"";
    if(args.count)
    {
        stringToSearch = [args valueForKey:@""];
    }
    else
    {
        //  error
        [self setScriptErrorNumber:-50];
        [self setScriptErrorString:@"Parameter Error......."];
    }
    // Implement your code logic
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AppShouldLookupStringNotification" object:stringToSearch];

    return [NSString stringWithFormat:@"result: %@", [NSDate date]];
}
@end

這就是你從Applescript捕獲通信的方式。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [textbox1 setStringValue:[NSString stringWithFormat:@"%05d", 1]];
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(getValueFromScript:)
     name:@"AppShouldLookupStringNotification"
     object:nil];
}

-(void)getValueFromScript:(NSNotification *)notification
{
    [yourTextbox setStringValue:[NSString stringWithFormat:@"from notification center %@", notification.object]];
}

下一步,我們需要一個Applescript來獲取/設置來自/到Cocoa應用程序的命令。

//In SDEF file we declared return type as array, here we created one.
set groceryList to {"eggs", "milk", "bread"} //the variable which you want to send to your app.

//Scripting language is pretty straight forward. If "named" window exists invoke the command and send return type to your Cocoa app.
tell application "Finder"
    activate
    if ((count of windows) > 0) then
        if name of front window is "YOUR_DESIRED_FINDER_WINDOW_NAME" then

            tell application "System Events"
                set running_apps to every application process's name
                if running_apps does not contain "YOUR_COCOA_APP_NAME" then
                    tell application "AppleScript Editor" to activate
                end if
            end tell

            tell application "AppleScript Editor"
                if it is running then
                    tell application "AppleScript Editor"
                        myFirstCommand groceryList //Updated this line. 
                    end tell
                end if
            end tell

        end if
    end if
end tell

當然,您還需要從Cocoa應用程序調用Applescript applet。 請看一下如何從cocoa App調用Applescript - regulus6633的答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM