【问题标题】:How to create new file in Finder and open it?如何在Finder中创建新文件并打开它?
【发布时间】:2022-10-26 04:26:17
【问题描述】:

出于某种原因,Mac 上的 Finder 不具备创建新文件的功能。我发现创建新文件的最快方法是将查找器路径拖到终端并在那里创建一个文件......这非常烦人

我编写了一个苹果脚本来自动创建一个新文件并将其绑定到一个快捷方式。它工作得很好,除了我不知道如何打开applescript 中的新文件。我很确定错误源于 POSIX / UNIX 路径,但即使我将 POSIX 放在文件、currentDir 等旁边,也找不到让它工作的方法。

这是我的脚本:

set file_name to display dialog "File Name" default answer "untitled" with icon note buttons {"Cancel", "Continue"} default button "Continue"
set file_name to (text returned of file_name)

tell application "Finder"
    set theWin to window 1
    set currentDir to POSIX path of (target of theWin as alias)
    
    make new file at (the target of the front window) as alias with properties {name:file_name}
    set currentPath to (currentDir & file_name) as string
    open file currentPath
end tell

该脚本创建文件,然后出错说它找不到文件来打开它。

【问题讨论】:

    标签: file applescript


    【解决方案1】:

    我稍微清理了代码并删除了不必要的行。

    set fileName to text returned of (display dialog "File Name" default answer ¬
        "untitled" with icon note buttons {"Cancel", "Continue"} default button 2)
    
    tell application "Finder" to set newFile to (make new file at Finder window 1 ¬
        with properties {name:fileName}) as alias
    
    delay 0.1
    do shell script "open -e " & quoted form of POSIX path of newFile
    

    注意:如果您尝试使用open -a 而不是open -e...并且收到错误消息,请尝试使用open -b。此选项允许您使用应用程序的捆绑标识符来标识要使用的应用程序,从而使用应用程序打开文件。

    例如,如果我想用Visual Studio Code.app通过使用它的包标识符...do shell script 命令看起来像这样...

    do shell script "open -b com.microsoft.VSCode " & quoted form of POSIX path of newFile
    

    如果您不知道如何获取应用程序在打开文件时使用的包标识符,下面的 AppleScript 代码允许您选择一个应用程序,然后将其包标识符复制到剪贴板。然后返回并将其粘贴到do shell script "open -b " 命令中。

    activate
    set chosenApp to name of (choose application with title ¬
        "Choose App" with prompt "Choose App")
    delay 0.1
    
    tell application chosenApp to set appID to id
    
    set the clipboard to appID
    

    请务必在 System Prefs 中授予 Terminal.app 适当的权限。

    注意:由于同时使用变量命名样式 underscore_naming 和 camelCaseNaming,因此在同一代码中被认为是不好的做法……我编辑了代码以仅使用 camelCaseNaming 变量命名样式。

    【讨论】:

    • 谢谢你的回答。我得到与其他问题相同的权限错误。尝试设置 FastScript 的可访问性(我如何运行工作流程),但这没有做任何事情。没有文件扩展名的文件与mac上的文本文件一样,所以如果它在脚本编辑器中打开不是问题,因为我可以更改默认应用程序。我希望能够创建 shell 文件、python 文件等。
    • @Alec我更新了帖子中的代码,并使用没有文件扩展名和文件扩展名“.txt”、“.sh”、“.py”和“.scpt”的文件进行了测试。 open -e 命令确保文件将使用 TextEdit.app 打开。终端中的命令man open 显示了可以与open 命令一起使用的不同选项。
    • 完美的。你知道为什么在末尾添加&“-a Visual Studio Code.app”并删除-e失败吗?
    • @Alec 我在我的帖子中添加了一些附加信息,这可能是解决您使用 open -a 命令时遇到的错误的好方法。
    • 感谢你的帮助。你知道 VSCode 的包标识符是什么吗?
    【解决方案2】:

    如果您打算使用 Finder 进行文件活动,最好避免使用 posix 路径。您的代码中有几个冗余,所以我对其进行了一些简化。

    set file_name to display dialog "File Name" default answer "untitled" with icon note buttons {"Cancel", "Continue"} default button "Continue"
    set file_name to (text returned of file_name)
    
    tell application "Finder"
        set theWin to window 1
        set currentDir to (target of theWin as text)
        --> "MacHD:Users:username:Hours of Operation:"
    
        make new file at theWin with properties {name:file_name}
        set currentPath to currentDir & file_name
        --> "MacHD:Users:username:Hours of Operation:untitled.txt"
        open file currentPath
    end tell
    

    试着把它放在上面(并注释掉上面的open file… 行)。

    tell application "System Events"
        activate application "TextEdit"
        open file currentPath of application "TextEdit" 
    end tell
    currentPath
    

    此外,在尝试上述操作后,将变量名 currentPath 作为脚本的最后一行(如我在上面添加的)并从脚本编辑器运行。它的值应该出现在“结果”中。您可以替换驱动器/用户名组件,但我想查看字符串的确切布局。

    有几点需要考虑:

    某些文件无法以这种方式打开,例如 jpeg。预览将拒绝打开一个空图像,大概是因为这不是图像的制作方式。复杂的文件格式(例如页面)也会失败。但是对于纯文本文件,应该没有任何问题。因此,我会考虑将“.txt”添加到您的默认文件名中。

    【讨论】:

    • 谢谢莫克曼。这更近了一步,但我得到了一个权限错误。该脚本已经具有 root 权限,我可以打开该文件 - 它只是不会从 applescript 打开
    • 什么版本的操作系统?
    • 我正在使用 montery (12.6)
    • 蒙特雷有一些影响某些行动的安全问题。您是从脚本编辑器运行脚本还是作为应用程序运行脚本?您可能需要授予可访问权限(在系统偏好设置中的安全和隐私中)。我使用的是更旧的操作系统(Sierra),所以我跟不上这些。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 2020-03-05
    • 2015-05-03
    • 2017-01-05
    相关资源
    最近更新 更多