今日の壁紙カレンダーを表示するAppleScript

今年のバージョンは出ないかなー、と思っていたら...出てました!三階ラボさんの2011年版の壁紙カレンダー。(感謝です!)早速ダウンロードさせてもらって、設定完了。...と思ったら、昨年とは若干、ファイルの命名規則が変更されたようで、次月・前月のAppleScriptが効かない模様。(はい、自分の問題です)

自分定義の連番ファイル名にする

そこで、今後のことも考えて、壁紙カレンダーのファイル名は自分定義の名前を連番で設定することにした。ファイル名の一括変更は、Automatorにそのまんまのアクションがあるので、それを使うとたいへん便利。

  • 以下は、Automatorでサービスを作った例。

  • 自分の場合は、以下のようにファイル名を連番設定することにした。
~/Pictures/desktop-calendar/desktop-calender-201101.png
~/Pictures/desktop-calendar/desktop-calender-201102.png
~/Pictures/desktop-calendar/desktop-calender-201103.png
  :
  :
~/Pictures/desktop-calendar/desktop-calender-201112.png
  • 以下に続く、翌月・前月・今日・年月日.appは、以下のルールで表現したファイル名を認識する。
    • 「ファイル名-yyyymm.png」(yyyymmは、西暦と月の6桁)
    • ファイル名の後にyyyymmを付加すること。
    • ファイル名とyyyymmの区切りには、-(ハイフン)、まはた_(アンダースコア)を利用可能。

翌月・前月AppleScript

    • 前月用のコメントを1行追記。(--display dialog result & my pic_name(-1) --前月.appの場合)



(* * 翌月.app * ファイルフォーマット:アプリケーションで保存する。
* デスクトップに置いて利用する。
*)
tell application "Finder"
try
set desktop picture to file (my pic_dir() & my pic_name(1)) --翌月.appの場合
--set desktop picture to file (my pic_dir() & my pic_name(-1)) --前月.appの場合
on error
activate
"以下のファイルが見つかりません。" & return & return
display dialog result & my pic_name(1) --翌月.appの場合
--display dialog result & my pic_name(-1) --前月.appの場合
end try
end tell

--例:desktop-calender-201001.png
on pic_name(num) tell application "Finder"
set file_name to (desktop picture as alias)'s name as text
set file_name_elements to my split(file_name, {"-", "_", "."}) set year_month to file_name_elements's item 3 as number
set year_month to year_month + num
if (year_month mod 100) > 12 then set year_month to year_month + 100 - 12
if (year_month mod 100) < 1 then set year_month to year_month - 100 + 12
set file_name_elements's item -2 to year_month
my join(file_name_elements's items 1 thru -2, "-") & "." & file_name_elements's item -1
end tell
end pic_name

on pic_dir() tell application "Finder"
(desktop picture as alias)'s folder as text
end tell
end pic_dir

(* on desktop_picture() tell application "System Events" current desktop's picture end tell end desktop_picture *)

on split(sourceText, delimiter) if sourceText = "" then return {} set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set theList to text items of sourceText
set AppleScript's text item delimiters to oldDelimiters
theList
end split

on join(sourceList, delimiter) set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set theText to sourceList as text
set AppleScript's text item delimiters to oldDelimiters
theText
end join

今日.app と 年月日.app を追加

今日.app
  • 起動すると、今月の壁紙カレンダーに切り替えて、今日.appアイコンを今日のマス目に移動する。
  • アプリケーションとして起動し続け、上記の状態を保持するように努める。(1分おきに監視)

つまり、常に今日のカレンダーを表示して、今日のマス目にアイコンを置くのだ。

  • 今日.app起動中のDockアイコンをクリックして、アイコン位置とマス目のサイズを設定すれば、どのサイズの壁紙カレンダーにも対応する。(たぶん)
  • デスクトップに置いて利用する。

    • アイコンがデスクトップの外側に配置される可能性を排除。



property cell : {w:96, h:96} --カレンダーのマスの大きさ
property o : {x:330, y:248} --カレンダー右上マスのアイコン座標

on run
my set_desktop_picture(current date) my set_icon_position(current date) end run

on idle
run
return 60
end idle

on reopen
try --キャンセルしなければ、このtryブロックが実行される
activate
"----- アイコンの位置調整 -----
カレンダー右上のマスに 今日.app を置いて、
「位置を調整する」ボタンを押してください。"
display dialog result buttons {"キャンセル", "位置を調整する"} tell application "Finder"
set p to desktop's file ("今日" & ".app")'s desktop position
end tell
activate
"カレンダーの1マスの大きさを入力して下さい。

1マスの大きさを確認するには...
 スクリーンショット(command-shift-4)で、
 十字カーソルをドラッグする方法をお勧めします。

(幅,高さ)"
display dialog result default answer "" & cell's w & "," & cell's h
my split(result's text returned, {","}) set cell to {w:result's item 1, h:result's item 2} --カレンダーのマスの大きさ
set o to {x:(p's item 1) - (cell's w) * 6, y:(p's item 2)} --カレンダー右上マスのアイコン座標
store script me in file ((path to desktop folder as text) & my name & ".app") replacing yes
on error err_msg number err_num
if err_num-128 then
error err_msg number err_num
end if
end try
run
end reopen



on set_desktop_picture(a_date) set {year:y, month:m, day:d, weekday:w} to a_date
set yyyymm to y * 100 + (m as number) tell application "Finder"
try
set desktop picture to file (my pic_dir() & my pic_name(yyyymm)) on error
activate
"以下のファイルが見つかりません。" & return & return
display dialog result & my pic_name(yyyymm) buttons {"キャンセル"} default button 1
end try
end tell
end set_desktop_picture

on set_icon_position(a_date) set {year:y, month:m, day:d, weekday:w} to a_date
set ym1 to date (y & "/" & (m as number) & "/" & 1 as text) --月初の日付
set ym1_w to ym1's weekday as number --月初の曜日の数値
set row to round (ym1_w - 2 + d) / 7 rounding down
set col to (w as number) - 1
tell application "Finder"
set desktop's file (my name & ".app")'s desktop position ¬ to my trim_position((o's x) + (cell's w) * col, (o's y) + (cell's h) * row) end tell
end set_icon_position

on pic_dir() tell application "Finder"
(desktop picture as alias)'s folder as text
end tell
end pic_dir

on pic_name(num) tell application "Finder"
set file_name to (desktop picture as alias)'s name as text
set file_name_elements to my split(file_name, {"-", "_", "."}) set file_name_elements's item -2 to num
my join(file_name_elements's items 1 thru -2, "-") & "." & file_name_elements's item -1
end tell
end pic_name

on trim_position(x, y) tell application "Finder"
set {desktop_x, desktop_y, desktop_w, desktop_h} to desktop's window's bounds
if x < desktop_x + 20 then set x to desktop_x + 20
if y < desktop_y + 20 then set y to desktop_y + 20
if x > desktop_w - 20 then set x to desktop_w - 20
if y > desktop_h - 20 then set y to desktop_h - 20
{x, y} end tell
end trim_position

on split(sourceText, delimiter) if sourceText = "" then return {} set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set theList to text items of sourceText
set AppleScript's text item delimiters to oldDelimiters
theList
end split

on join(sourceList, delimiter) set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set theText to sourceList as text
set AppleScript's text item delimiters to oldDelimiters
theText
end join

年月日.app
  • 年月日を入力して、入力した日付の壁紙カレンダーに切り替え、年月日.appアイコンを入力した日付のマス目に移動する。
  • アイコン位置とマス目のサイズは、今日.appの設定を利用している。
  • デスクトップに置いて利用する。



(* * 年月日.app * ファイルフォーマット:アプリケーションで保存する。
* デスクトップに置いて利用する。
*)
set TD to load script file ((path to desktop folder as text) & "今日.app")

set myname to my name
set yyyymmdd to {(current date)'s year, (current date)'s month as number, (current date)'s day}

activate
display dialog "年月日を入力してください。" default answer TD's join(yyyymmdd, "/")
set returned_date to date (result's text returned)

TD's set_desktop_picture(returned_date)
TD's set_icon_position(returned_date)


今年は、常に今日の壁紙カレンダーが自動表示されるようになった!(つもり)しばらく使ってみよう。