天天看點

AppleScript - 調用 Pages 将檔案導出為 docx

文章目錄

    • 代碼實作
    • 關于 POSIX-style
    • 相關資料

python 無法直接操作 doc,隻能操作 docx;

win2com 可以将 doc 轉化為 docx,可惜無法在 linux/macOS 下安裝 win32com;

使用 Pages 打開 doc,可以導出到 docx;

然而手動導出注定是繁瑣的,這時想到可以使用 AppleScript 來操作 Pages

代碼實作

如下代碼實作了将 pages 檔案導出為 docx

tell application "Pages"
	set thisDoc to open "/Users/user/Desktop/奧運會.pages" 
	export thisDoc to POSIX file "/Users/user/Desktop/a.docx" as Microsoft Word
	delay 2
	close thisDoc
end tell
           

關于 POSIX-style

很多使用者出現各種問題,是因為 file path 前面沒有添加 POSIX;

/Users/user/Desktop/a.docx

是一個 POSIX 風格路徑;

Some scriptable apps are designed to work with POSIX-style paths, rather than AppleScript alias and file objects. Like a file object, a POSIX file object is not dynamic and can also refer to an item that doesn’t exist yet.

A POSIX file object is displayed as a slash-delimited path preceded by a POSIX file specifier, in the format shown in Listing 15-10.

相關資料

  • Referencing Files and Folders in AppleScript

    https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ReferenceFilesandFolders.html#//apple_ref/doc/uid/TP40016239-CH34-SW1

繼續閱讀