@nak435

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

Apple Script で Mac Safari 「PDFとして書き出す...」の「保存」ボタンを押したい

Apple Script で Mac Safari 「PDFとして書き出す...」ダイアログ の「保存」ボタンを押したいが書き方が分かりません。

macOS 26.1 (25B78)
Safari 26.1 (21622.2.11.11.9)

URL-001.png

以下のスクリプトで、ファイル名のペーストはできましたが、「保存」ボタンが押せません。(カッと、キー入力の無効音みたいな音が鳴る)

set filename to "URL-001"
set the clipboard to the result as text

tell application "Safari" to activate
delay 0.1
tell application "System Events"
	keystroke "p" using {command down, shift down} -- shortcut of "PDFとして書き出す..."
	delay 0.2
	keystroke "v" using command down -- paste to filename
	delay 0.1
	keystroke return -- button "保存"
end tell

Safari「PDFとして書き出す...」に、ショートカット⇧⌘P を割り当て済み

keystroke returnを次のように書くと、エラーになります。

	tell button "保存" of splitter group 1 of sheet 1 of window Document of application "Safari"
		keystroke return -- button "保存"
	end tell

399:415: execution error: Safariでエラーが起きました: documentのタイプをintegerに変換できません。 (-1700)

document 1 を指定してもエラー。

	tell button "保存" of splitter group 1 of sheet 1 of document 1 of application "Safari"
		keystroke return -- button "保存"
	end tell

394:410: execution error: Safariでエラーが起きました: sheet 1 of document 1を取り出すことはできません。 (-1728)

Window 1 とか 2 を指定してもエラー。

	tell button "保存" of splitter group 1 of sheet 1 of window 1 of application "Safari"
		keystroke return -- button "保存"
	end tell

388:409: execution error: Safariでエラーが起きました: sheet 1 of window 1を取り出すことはできません。 (-1728)

「PDFとして書き出す」ダイアログに関連するUIエレメントは、次のとおりです。

sheet 1 of window Document of application process Safari
splitter group 1 of sheet 1 of window Document of application process Safari
static text 場所: of splitter group 1 of sheet 1 of window Document of application process Safari
pop up button 場所: of splitter group 1 of sheet 1 of window Document of application process Safari
UI element 3 of splitter group 1 of sheet 1 of window Document of application process Safari
static text 名前: of splitter group 1 of sheet 1 of window Document of application process Safari
text field 名前: of splitter group 1 of sheet 1 of window Document of application process Safari
static text タグ: of splitter group 1 of sheet 1 of window Document of application process Safari
text field タグ: of splitter group 1 of sheet 1 of window Document of application process Safari
button キャンセル of splitter group 1 of sheet 1 of window Document of application process Safari
button 保存 of splitter group 1 of sheet 1 of window Document of application process Safari

参考


色々試してみたのですが、どうにも書き方が分かりませんでした。
AppleScript 詳しい方がおられましたら、教えていただけると幸いです。

0 likes

1Answer

次のスクリプトで期待する動作となりました。自己解決

set filename to "URL-001"
set the clipboard to the result as text

tell application "Safari"
    activate
    delay 0.1
    tell application "System Events" to tell process "Safari"
        click menu item "PDFとして書き出す…" of menu "ファイル" of menu bar 1
        delay 0.1
        keystroke "v" using command down -- paste to filename
        delay 0.1
        click button "保存" of splitter group 1 of sheet 1 of window 1 
        delay 0.1
    end tell
end tell
0Like

Your answer might help someone💌