それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

アプリがMac OSXのサンドボックについて確認

OSXサンドボックスで動いてると。。。

AppleScriptAutomatorで、動作が「期待通り」にならない。サンドボックスの中でアクセス可能なフォルダが制限される。。。

本当に由々しき問題だと思う。アプリ側の実装バグなのか、サンドボックスに由来するものかちょっと調べるには。アプリがsandboxing されているか調べる必要があった。

アクティビティ・モニタでサンドボックスされているか確認できる

f:id:takuya_1st:20160129190859p:plain

参考資料

http://yuzuhara.hatenablog.jp/entry/2011/12/27/235521

http://eyeballonly.com/blog/2014/02/20/macosx-sandbox/

minecraft AtLauncher で スクリーンショットの場所

スクショの場所がわからない。。。。

調べたら、通常のMinecraftの位置ではなく、インスタンスごとに保存される模様。

場所は、AtLauncherの中。

$PATH_TO_AtLancher/$INSTANCE_NAME/screenshots/

Mac OSX なら、次の場所にあった。

/Applications/ATLauncher.app/Contents/Resources/Java/Instances/BevosTechPack/screenshots

とりあえず、ショートカットでも作っておけば便利なんでしょうか。

参考資料

https://www.reddit.com/r/ATLauncher/comments/32y6zn/screenshots/

Apple Script でファイル保存(osascript/osjscript)

osascript でファイルを保存するサンプル

ファイルを保存するという基本的なところをUI Elementsではなく、Standard Suiteで行うことにした。

tell application ("TextEdit")
activate
make new document
set text of document 1 to "test from apple script"
 save document 1 in POSIX file ("/Users/takuya/Desktop/sample.txt")
end tell

同じことをJavascript for Automation で

var app = Application("TextEdit")
app . activate()
var doc  = app.Document()
doc.text = "test"
app.documents.push(doc)

app.documents[0].save({
in : Path("/Users/takuya/Desktop/sample2.txt")
})

こんなもん。簡単でした。

保存については、マニュアル飲み方

f:id:takuya_1st:20160129200405j:plain:w400

ポイント

関数の引数の渡し方

JavaScriptで書くときには、ちょっとくせがる。

引数なしの時

doc.save() //

引数があるとき

doc.save({ as : 'Text' }) //

引数は、不定個数ではなく、「一つ」で渡す。その一つはJSONであり、引数の名前が、キー名になる。

引数を複数入れるとき

doc.save({ as : 'Text' in: Path("/Path/to/Write") }) //

オブジェクトで複数いれます。ファイル・パスはPath()オブジェクトを使ってUNIXパス文字列を扱います。

理屈がわかると簡単

JavaScriptでもAppleScriptでもドキュメントさえ押さえれば、簡単に自動化処理を作ることができる。

AppleScript で PPTxをPDFに変換する。

たくさんある、PPTをPDFに変換したいと思った。

まぁ、PPT→PPTXでもインだけど、PDFにしちゃう方がポータビリティがいいんですよね。

どうせ、PPTXを編集することなんてないんだし。

PDFの方が、Evernoteに放り込んだり、検索可能にしたり、Explorer/Finderのプレビューでパパッと見られて色々便利なんだよね。

keynote を使う場合。

開いているファイルを、PDFに変換して保存する

var app = Application("Keynote")
var doc  = app.Document()
//doc.text = "test"
//app.documents.push(doc)
app.documents[0].save({
in : Path("/Users/takuya/Desktop/sample2.key")
})

console.log(app.documents[0].name())
app.documents.at(0).export
app.documents.at(0).export({
as : "PDF",
 to : Path("/Users/takuya/Desktop/sample2.pdf")
})

これで、pptx を keynotes 形式に変換したり、keynotes 形式をPDFに変換したりできる。

楽チンですが・・・MicrosoftのOfficeで変換したときと若干異なるんですよね。

PowerPoint だと動かない

同じように やったけど動かないんだ。

var app = Application("Microsoft PowerPoint")
var doc = app.presentations.at(0)

console.log(doc.name())

doc.save({
as : "PDF",
in : "/Users/takuya/Desktop/sample2.pdf"
})

AppleScript だと辛うじてうごく

set out to POSIX file "/Users/takuya/Desktop/test.pdf"
tell application "Microsoft PowerPoint"
save active presentation in out as save as PDF
end tell

ダイアログが出てくるのだが、動いてるとは思えない。

f:id:takuya_1st:20160129202811p:plain:w200

もう半年くらい、この原因とたかってて

ある日、ひらめいた。Sandbox 化が原因だったんじゃ?

とりあえずサンドボックス領域を調べてみた。

ファイルができてるではないか

f:id:takuya_1st:20160129202739p:plain:w400

ということで、AutomatorApplescriptPowerpointで変換したら、次のパスに作られる。

~/Library/Containers/com.microsoft.Powerpoint/Data.pdf

該当資料を探した。

http://answers.microsoft.com/en-us/mac/forum/macoffice2016-macexcel/applescript-save-as-pdf-fails/bccc7a27-0263-44be-9506-deb7d3de387e?auth=1

After studying the Document "App Sandbox in Depth" it seems just consequent for me what Microsoft did with Office 2016 and AppleScript. Regarding this document sandboxed apps can just write automatically (triggered by AppleScript) within the path ~/Library/Containers/com.microsoft.excel/. So doing something like this
tell application "Microsoft Excel"
    tell active sheet
        save in path to temporary items from user domain as PDF file format
    end tell
end tell
should work. Files can afterwards be found in ~/Library/Containers/com.microsoft.excel/Data/Library/Caches/TemporaryItems/

ほかにも

http://eyeballonly.com/blog/2014/02/20/macosx-sandbox/

http://piyocast.com/as/archives/category/applescriptobjc/nshomedirectory/

などで、散見された。

Sanbox 化されたアプリケーションへのAutomator・ScriptEditorについて

https://developer.apple.com/library/mac/qa/qa1888/_index.html

Sandboxで、アクセス可能なディレクトリにDesktopがない。アプリ側でパーミッションを要求する感じ。でも動いてないんだよなぁ

しかしながら「テキストエディット」のような、Apple謹製のAppは、Sandboxされているが、Desktopへファイル保存が可能である。

ということは、Microsoftのプログラム側にバグがある。マイクロソフトPowerPointのプログラムでSandboxを正しく扱えていないということしか考えられない。