Apple Script Page

Macでの単純作業を自動化

Apple Script でNumbersの指定したセルに値を入力してファイルをPDF保存する

Apple Scriptについて調べることとなった始まりは、Numbersで作った自分用手帳(下よりダウンロード可)でVlookupを使って年間の予定から1日の予定を引っ張るために指定したセルに自動で値を入れるにはどうしたらいいかと悩んでいたときに、Apple Scriptが良さそうだということがわかり、それ以来少しずつ学んでいます。

ショートカットアプリで自動化しようと最初は考えていたのですが、ショートカットアプリでNumbersに実行できるのは、指定したファイルを開くことぐらいでした。

本当は指定したシートのみをPDFに書き出したいのですが、まだ勉強中です。取り急ぎバックアップのために全シートをPDFに変換して毎日の連絡を漏れなくできるようにしています。

ショートカットアプリで指定したファイルを開く指示を出してから。下記のApple Scriptを実行しています。
set targSheetName to "today"
tell application "Numbers"
	if (count every document) = 0 then return false
	tell front document
		set sEx to exists of sheet targSheetName
		if sEx = true then
			set active sheet to sheet targSheetName
		end if
		tell active sheet
			try
				set theTable to first table whose class of selection range is range
			on error
				
				set tCount to count every table
				if tCount = 0 then error "There is no table in active sheet"
				set theTable to first table
				tell theTable
					set selection range to cell "A1"
				end tell
			end try
		end tell
	end tell
end tell
tell application "Numbers"
	tell active sheet
		tell theTable
			tell cell "A1"
				set value to date string of ((current date) + 86400)
				delay 1
			end tell
		end tell
	end tell
end tell

set desktopFolder to (path to desktop) as text
tell application "Numbers"
	activate
	set docName to name of front document
	set PDFExportFileName to desktopFolder & docName & ".pdf"
end tell

tell application "Finder"
	try
		make new file at desktop with properties {name:(docName & ".pdf")}
	end try
end tell
tell application "Numbers" to export front document to file PDFExportFileName as PDF

Microsoft Wordを一括でPDFに変換する

ロイロノートにてWordで作成した教材を共有したいときに、このScriptを使っています。作業の時間短縮となり、かなり重宝しています。コードは次のAppleのページから引用しています。ショートカットアプリで”Finderで選択されたファイルを取得”の次のフローにApple Scriptを実行の中に以下のコードを貼り付ければ可能です。

Trying to convert Word 2016 doc to PDF. T… - Apple Community
use scripting additions

on run {input, parameters}
	repeat with aFile in input
		tell application "System Events"
			set inputFile to disk item (aFile as text)
			set outputFileName to (((name of inputFile) as text) & ".pdf")
		end tell
		
		tell application id "com.microsoft.Word"
			activate
			open aFile
			tell active document
				save as it file name outputFileName file format format PDF
				close saving no
			end tell
			set defaultPath to get default file path file path type documents path
		end tell
		
		tell application "System Events"
			set outputPath to (container of inputFile)
			set outputFile to disk item outputFileName of folder defaultPath
			move outputFile to outputPath
		end tell
	end repeat
	return input
end run
タイトルとURLをコピーしました