macos - Sketch App 的 Apple 脚本

标签 macos cocoa applescript scripting-bridge sketch-3

我正在尝试为 Sketch.app (com.bohemiancoding.sketch3) 编写一个 Apple 脚本。我想要做的是,创建一些可以从 Sketch 文档在浏览器中渲染的图像文件。

当我在Script Editior中打开Sketch.app字典时,我明白了

saveable file format enum
    Sketch : The native Sketch 2 file format
    PDF : Portable Document Format
    TIFF : Tagged Image File Format

所以我考虑使用以下脚本生成 TIFF,但它不起作用

tell application "Sketch"
  set curdoc to document 0    
  save curdoc in "/Users/mirza/Downloads/mew2" as TIFF
end tell

我可以使用保存命令创建 .sketch 格式的草图副本,但不能创建 PDF 或 TIFF。 sketch是否支持使用Apple脚本的PDF和TIFF?

或者还有其他办法吗?

更新

我将路径更改为苹果脚本格式并将文档索引设置为 1。现在脚本如下所示

set thisFilePath to (POSIX file "/Users/mirza/Downloads/mew2")
log thisFilePath
tell application "Sketch"
    curdoc to document 1
    save curdoc in thisFilePath as TIFF -- Tried with quotes as well, gives same error
end tell

但是当我运行脚本时出现以下错误

Result:
error "Sketch got an error: Can’t continue curdoc." number -1708

更新2

修正错别字

set thisFilePath to (POSIX file "/Users/mirza/Downloads/mew2")
log thisFilePath
tell application "Sketch"
    set curdoc to document 1
    log (path of curdoc)
    save curdoc in thisFilePath as "TIFF"
end tell

但是当我运行脚本时出现以下错误

Result:
error "Sketch got an error: The document cannot be exported to the \"TIFF\" format." number -50

最佳答案

您的代码存在很多问题,但是,首先,您会发现使用不再可用的软件很难获得明确的答案。 Sketch 版本 3 已经有一段时间了,AppleScript 字典可能已经改变。 话虽如此,以下是关于您的代码的一些想法:

如果 Sketch 2 AS 字典是这样读取的,那么 AS 功能在 v3 中已发生变化。 我很想帮忙,但我在任何地方都找不到 v2,所以我只能在黑暗中这样做。

set thisFilePath to choose file name--use this to select a new file;
------- a Mac AppleScript path is returned (a file specification,
------- actually, which is different from a string or alias
------- (but an alias is kind of like a file spec)
tell application "Sketch"
    set curdoc to document 1--not zero-based; 1 is frontmost doc
    save curdoc in thisFilePath as "TIFF"--*this is a guess
end tell

所以,我不知道最后一个 save 行会做什么,但它可能会起作用。 在 Sketch 3 中,保存时不允许使用“TIFF”格式,但它确实有一个 as 参数作为保存的一部分,该参数应该与表示格式的文本字符串(如上面的“TIFF”)。 Sketch 2 似乎有不同的方案(带有 as 的参数不是字符串)。如果我在 Sketch 3 中不使用 as 参数进行保存,它将以 Sketch 的 native 格式保存。所以你可以尝试不带引号(就像你一样)。我只是按照 v3 字典告诉我的去做。 以下是一些解决方案和提示:

  1. 文档 1 应该能够引用最前面的文档;

  2. 如果您出于某种原因想要使用 POSIX 写出您的路径 (就像你所做的那样),你可以使用

    POSIX 文件“/Users/mirza/Downloads/mew2”

返回 AppleScript 的 Mac 样式路径,其形式如下:

"yourHardDriveName:Users:mirza:Downloads:new2"

您还可以通过执行以下操作来获取我这里的“yourHardDriveHame:”

tell application "Finder" to set sDr to startup disk as string

然后通过执行来连接

sDr & "Users:mirza:Downloads:new2"

你也可以这样做

tell application "Finder" to set myHome to home as string

这应该返回主文件夹的 Mac 风格路径。 (是的,Finder 还允许您获取其他路径)。

有一些东西可以玩。

关于macos - Sketch App 的 Apple 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32601184/

相关文章:

ios - 遵循 iOS 设计模式和 Mac OSX 编程

objective-c - 如何在 Cocoa Mac os 开发中使用菜单项使鼠标悬停在 NSControlSegment 上?

objective-c - 关于 Cocoa C 风格函数的一些愚蠢问题

MacOS Automator + Applescript 用于将 docx 导出为 pdf 的解决方案

terminal - 如何在applescript终端中更改带有空格的目录?

bash - OSX 将 shell 脚本与文件扩展名相关联?

ios - CFHTTPMessageAddAuthentication 无法将身份验证数据添加到请求

macos - macOS High Sierra 10.13 上的 PDFtk 服务器

swift - 如何在NSDatePicker中正确使用rangeDataMode?

cocoa - “托管对象类生成”对话框中的 "Generate Validation Methods"复选框有什么作用?