雑多なアップルスクリプト



DVDプレーヤーで指定した時間へ飛んで再生する

CotEditorでソートする

全角英数字を半角英数字に変換

スクリーンショットに日時をつけて保存

拡張子を除いたファイル名を取得

消えてしまったSpotlightコメントを復活

クリップボードのテキストのスタイルを削除



★ DVDプレーヤーで指定した時間へ飛んで再生するアップルスクリプト

標準でついてる「Go To Time...」だと、時間を入力する際に、1時間14分35秒なら「01:14:35」のように入力しなければならず面倒(「11435」と入力すると「11435秒」と解釈される)。このスクリプトなら「11435」と入力すれば1時間14分35秒に、「845」と入力すれば8分45秒に飛べるのでコロンを入力する手間が省けます。

「スクリプト」形式で保存して、「ホーム/ライブラリ/Application Support/DVD Player Scripts」に入れてください。

tell application "DVD Player"
	set et to elapsed time
	set h to round (et / 3600) rounding down
	set et to et - 3600 * h
	set m to round (et / 60) rounding down
	set s to et - 60 * m
	
	set t to text returned of (display dialog "Go to time" default answer (h & m & s as string))
	if t as number < 10000 then set t to ("0" & t as string)
	if t as number < 1000 then set t to ("0" & t as string)
	
	set {h, m, s} to {(character 1 of t), (character 2 of t & character 3 of t), (character 4 of t & character 5 of t)}
	if (h as number) > 0 then set h to (3600 * h)
	if (m as number) > 0 then set m to (60 * m)
	set elapsed time to h + m + (s as number)
end tell


★ CotEditorでソートするアップルスクリプト

テキストエディタのCotEditorでソートを行なうアップルスクリプトです。入力の仕方がちょいとややこしいですが、通常のソートならそのままOKでできます。

ちなみに、以下のように改変すればOSX標準のテキストエディットでもソートできますが、フォントサイズなどのテキストスタイルが失われてしまいます。

1行目 tell application "CotEditor" → tell application "TextEdit"

下から4行目 if line ending of front document 〜の行を削除

--ソート
tell application "CotEditor"
	
	set {the_key, the_rev} to {text returned, button returned} of (display dialog "記入例" & return & "1文字目でソート → 1" & return & "セパレーターが/、その後の3文字目 → 3,/" & return & "セパレーターがタブ、その後の1文字目 → 1,\\t" default answer "1" buttons {"キャンセル", "降順", "昇順"} default button 3)
	if the_rev is "昇順" then set the_rev to ""
	if the_rev is "降順" then set the_rev to "-r "
	
	set the_sep to ""
	if the_key contains "," then
		set the_num to do shell script "echo " & quoted form of the_key & " | sed s/,.*//"
		set the_sep to do shell script "echo " & quoted form of the_key & " | sed s/" & the_num & "," & "//"
	end if
	
	if the_sep is not "" then
		set the_key to " -t " & quoted form of the_sep & " -k2." & the_num
	else
		set the_key to " -k1." & the_key
	end if
	
	--改行コードがCRの場合LFに変換
	set cr_to_lf to ""
	if line ending of front document is CR then set cr_to_lf to " | tr '" & (ASCII character 13) & "' '" & (ASCII character 10) & "'"
	
	set the_contents to text of front document
	set sorted_contents to do shell script "echo " & quoted form of the_contents & cr_to_lf & " | sort " & the_rev & the_key
	
	set text of front document to sorted_contents
	
end tell


★ 全角英数字を半角英数字に変換するアップルスクリプト

全角英数字を半角英数字に変換します。

このスクリプトでは元の文字列をzentextという変数に入れ、全角、半角の変換用テーブルで比較・変換し、変換した文字列をhantextという変数に入れています。zentable、hantableがそれぞれ全角、半角の変換用テーブル。これを応用すれば半角カナ全角カナ変換、ひらがなカタカナ変換なども作れます。これは一文字対応テーブルですが、テーブルの内容をリスト形式にすれば語単位での変換も可能になります。

テーブルに文字を追加・削除したいときは、全角半角それぞれの文字が対応する順番に注意して(一部の半角文字はエスケープ記号「\」を含んでいます)。

--set zentext to text returned of (display dialog "全角→半角" default answer "") --テスト用ダイアログ
set zentext to "全角Text 1" --これは例。文字列は任意で。

set zentable to " !?#$¥%&@.,:;()[]{}/\_|^`〜’”+−<=>*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
set hantable to " !?#$\¥%&@.,:;()[]{}/\\_|^`~'\"+-<=>*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
set hantext to ""
considering case
	repeat with x in zentext
		set y to 1
		set convtext to x as string
		if convtext is in zentable then
			repeat with x in zentable
				if convtext contains (item y of zentable) then
					set convtext to (item y of hantable) as string
					exit repeat
				end if
				set y to y + 1
			end repeat
		end if
		set hantext to hantext & convtext
	end repeat
end considering
hantext --半角に変換されたテキスト。


★ スクリーンショットに年月日時分秒をつけて保存するアップルスクリプト

do shell script "screencapture ~/Desktop/" & "ScreenShot-" & (do shell script "date +%Y%m%d%H%M%S") & ".png"


★ 拡張子を除いたファイル名を取得するアップルスクリプト

tell application "Finder"
	set PathName to (choose file) --仮に
	set ExtName to name extension of PathName
	set PathName to POSIX path of PathName
	set FileName to (do shell script "basename -s ." & ExtName & " " & quoted form of PathName)
end tell


★ 消えてしまったSpotlightコメントを復活させるアップルスクリプト

MacOSX10.6でなぜか突然消えてしまうことが多いSpotlightコメントを復活させるスクリプトです。ただし100%復活できる保証はありません。

tell application "Finder"
	set slct to (selection)
	repeat with x in slct
		set thepath to POSIX path of (x as alias)
		set spcomment to (do shell script "mdls -name kMDItemFinderComment " & quoted form of thepath & "| awk -F\\\" '{print $2}'")
		if spcomment is "" then
			display dialog "コメントなし"
		else
			set comment of x to spcomment
		end if
	end repeat
end tell


★ クリップボードのテキストのスタイルを削除するアップルスクリプト

do shell script "pbpaste | pbcopy"