applescript - 使用 JXA AppleScript 获取事件 Finder 窗口的 POSIX 路径

标签 applescript macos-sierra finder javascript-automation

我想要这个 AppleScript 片段的 JXA 等价物:

tell application "Finder"

    # Get path
    set currentTarget to target of window 1
    set posixPath to (POSIX path of (currentTarget as alias))

    # Show dialog
    display dialog posixPath buttons {"OK"}

end tell

我得到的最接近的是使用 url初始化 Foundation NSURL 的属性对象并访问其 fileSystemRepresentation属性如下:

// Get path
var finder = Application('Finder')
var currentTarget = finder.finderWindows[0].target()
var fileURLString = currentTarget.url()

// I'd like to get rid of this step
var fileURL = $.NSURL.alloc.initWithString(fileURLString)
var posixPath = fileURL.fileSystemRepresentation

// Show dialog
finder.includeStandardAdditions = true
finder.displayAlert('', {buttons: ['Ok'], message: posixPath})

但这似乎不必要地复杂。有没有更好的方式 到达 POSIX 路径 不使用 Foundation API 或手动字符串争吵?

System Events AppleScript Dictionary

如果我天真地尝试这个:

finder.finderWindows[0].target().posixPath()

我收到此错误:

app.startupDisk.folders.byName("Users").folders.byName("kymer").folders.byName("Desktop").posixPath()
        --> Error -1728: Can't get object.

SO answer似乎相关,但我似乎无法适应我的需求:

App = Application.currentApplication()
App.includeStandardAdditions = true
SystemEvents = Application('System Events')

var pathToMe = App.pathTo(this)
var containerPOSIXPath = SystemEvents.files[pathToMe.toString()].container().posixPath()

任何帮助将不胜感激!

最佳答案

这么简单的一段 AppleScript 代码没有直接的 JXA 翻译,这一事实证明了 基于 OSA 脚本编写的 JXA 和 macOS 自动化的遗憾状态 :

  • foo's excellent answer包含有用的背景信息。
  • 另一个指针是the last time release notes were published was for OS X 10.11 (El Capitan) (在撰写本文时,我们即将发布 macOS 10.13(High Sierra))。
  • This third-party July 2017 blog post更广泛地宣布“这个问题终于在上个月的 WWDC 上得到了回答: 苹果已经放弃了它的自动化技术,让它们枯萎死亡。

  • 正如你自己的例子所表明的,在两种濒临灭绝的自动化脚本语言中 AppleScript - 尽管有很多缺点 - 是更成熟、更可靠的选择 .

    为了解决您在 JXA 中的问题,您似乎自己想出了最好的方法。
    让我把它打包成 辅助函数这可能在某种程度上减轻了痛苦 - 需要明确的是:这样的辅助功能不应该是必要的:

    // Helper function: Given a Finder window, returns its folder's POSIX path.
    // Note: No need for an ObjC.import() statement, because NSURL is 
    //       a Foundation class, and all Foundation classes are implicitly
    //       available.
    function posixPath(finderWin) {
      return $.NSURL.alloc.initWithString(finderWin.target.url()).fileSystemRepresentation
    }
    
    // Get POSIX path of Finder's frontmost window:
    posixPath(Application('Finder').finderWindows[0])
    

    关于applescript - 使用 JXA AppleScript 获取事件 Finder 窗口的 POSIX 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45426227/

    相关文章:

    ios - 如何从 Mac OS X 应用程序获取 iPod/iPhone iOS 版本?

    audio - 在 QT 7 pro 中更改 Soundtrack 属性 - 使用 applescript 批处理

    python - 使用python从网络接口(interface)读取和处理数据包

    macos - 如何永久安装Visual Studio Code的 `code`命令?

    objective-c - 如果您必须为文件夹使用自定义图标,您如何以编程方式将文件夹图标放在 Finder 侧边栏上?

    python - 使用 applescript (重新)启动 python 脚本

    macos - 如何在 os x 中使用 Applescript 应用程序包作为默认浏览器?

    macos - 以编程方式访问网络浏览器选项卡 | swift 3

    swift - swift 中的 "Ask for Finder Items"

    macos - 将您自己的 NSMenuItem 添加到 Finder 菜单