« [Acrobat]全ファイル全ページ印刷 | トップページ | [Acrobat]メタデータ書き込み...挫折...【解決】 »

[AppleScript]日付の基本

AppleScriptでの日付の整形方法のヒント


621x437_currentdate_03

↑このような戻り値になるのは

↓書式の設定が反映されているのですが
665x292_currentdate_05

 

内部的には英語で動作しています。
621x437_currentdate_09


663x291_currentdate_08


 

そんな事になっていますので
今日の日付の変換は英語で行なえます。





set nowTime to my DateAndTIme(current date)


------------------#ここからサブルーチン
to DateAndTIme(theDate)
set y to (year of theDate)
set m to my monthNumStr(month of theDate)
set d to day of theDate
set hms to time of theDate
set hh to h of sec2hms(hms)
set mm to m of sec2hms(hms)
set ss to s of sec2hms(hms)
return (y as text) & my zero1(m) & my zero1(d)

end DateAndTIme
------------------#月表示を数字に置き換える
to monthNumStr(theMonth)
set monList to {January, February, March, April, May, June, July, August, September, October, November, December}
repeat with i from 1 to 12
if item i of monList is theMonth then exit repeat
end repeat
return i
end monthNumStr
------------------#ゼロサプレス
to zero1(n)
if n < 10 then
return "0" & n
else
return n as text
end if
end zero1
------------------#時間表示を:で分割
to sec2hms(sec)
set ret to {h:0, m:0, s:0}
set h of ret to sec div hours
set m of ret to (sec - (h of ret) * hours) div minutes
set s of ret to sec mod minutes
return ret
end sec2hms


このようにしておく事で
『ユーザーの環境設定』に左右されないって
事になりますね。

 

「yyyymmdd.rtf」をダウンロード[Download]DownloadFile

「yyyymmdd.scpt.zip」をダウンロード
[Download]DownloadFile

 

日時関係のオプションはだいたい
こんな感じ


tell current application

set DateAndTIme to my (current date)

set nowYear to my year of (current date)

set nowMonth to the month of (current date)

set nowMonthNo to the month of (current date) as number

set nowDay to the day of (current date)

set nowWeekday to the weekday of (current date)

set nowWeekdayNo to the weekday of (current date) as number

set nowHours to the hours of (current date)

set nowMinutes to the minutes of (current date)

set nowSecond to the seconds of (current date)

set nowTime to the time of (current date)

set nowDateString to the date string of (current date)

set nowTimeString to the time string of (current date)

set nowDateList to the {year, month, day} of (current date)

set nowDay to the (DateAndTIme) + (2 * days)

set nowDay to the (DateAndTIme) + (2 * hours)

end tell

戻り値は実際に試してみてください。

 

例えば24時間後を求めるなら

tell current application

set DateAndTIme to my (current date)
set nowDay to the (DateAndTIme) + (24 * hours)

end tell


60日後
って事なら

tell current application

set DateAndTIme to my (current date)
set nowDay to the (DateAndTIme) + (60 * days)

end tell

になります。


 

「now.rtf」をダウンロード[Download]DownloadFile

「now.scpt.zip」をダウンロード[Download]DownloadFile

 

日付だけでなく
時間も入れたい場合は
このようになりますね。




set nowTime to my DateAndTIme(current date)


--------------------------------------------------#ここからサブルーチン
to DateAndTIme(theDate)
set y to (year of theDate)
set m to my monthNumStr(month of theDate)
set d to day of theDate
set hms to time of theDate
set hh to h of sec2hms(hms)
set mm to m of sec2hms(hms)
set ss to s of sec2hms(hms)
return (y as text) & my zero1(m) & my zero1(d) & "_" & zero1(hh) & zero1(mm) & zero1(ss)

end DateAndTIme
------------------#月表示を数字に置き換える
to monthNumStr(theMonth)
set monList to {January, February, March, April, May, June, July, August, September, October, November, December}
repeat with i from 1 to 12
if item i of monList is theMonth then exit repeat
end repeat
return i
end monthNumStr
------------------#ゼロサプレス
to zero1(n)
if n < 10 then
return "0" & n
else
return n as text
end if
end zero1
------------------#時間表示を:で分割
to sec2hms(sec)
set ret to {h:0, m:0, s:0}
set h of ret to sec div hours
set m of ret to (sec - (h of ret) * hours) div minutes
set s of ret to sec mod minutes
return ret
end sec2hms


 


「yyyymmdd_hhmmss.rtf」をダウンロード[Download]DownloadFile

「yyyymmdd_hhmmss.scpt.zip」をダウンロード[Download]DownloadFile

 


デスクトップフォルダに『今日の日付』のフォルダ作ったり

今日の日付のファイル名に変更したり


色々工夫できますね。




set nowTime to my DateAndTIme(current date)

tell application "Finder"
try
make new folder at (path to desktop folder from user domain as alias with folder creation) with properties {name:nowTime}
end try
end tell

--------------------------------------------------#ここからサブルーチン
to DateAndTIme(theDate)
set y to (year of theDate)
set m to my monthNumStr(month of theDate)
set d to day of theDate
set hms to time of theDate
set hh to h of sec2hms(hms)
set mm to m of sec2hms(hms)
set ss to s of sec2hms(hms)
return (y as text) & my zero1(m) & my zero1(d)

end DateAndTIme
------------------#月表示を数字に置き換える
to monthNumStr(theMonth)
set monList to {January, February, March, April, May, June, July, August, September, October, November, December}
repeat with i from 1 to 12
if item i of monList is theMonth then exit repeat
end repeat
return i
end monthNumStr
------------------#ゼロサプレス
to zero1(n)
if n < 10 then
return "0" & n
else
return n as text
end if
end zero1
------------------#時間表示を:で分割
to sec2hms(sec)
set ret to {h:0, m:0, s:0}
set h of ret to sec div hours
set m of ret to (sec - (h of ret) * hours) div minutes
set s of ret to sec mod minutes
return ret
end sec2hms

 


「yyyymmdd__make_folder.scpt.zip」をダウンロード[Download]DownloadFile

「yyyymmdd__make_folder.scpt.rtf」をダウンロード[Download]DownloadFile


 
ワンポイントで
月番号

曜日番号

as number
とする事で数値でも表示されます。


Screencapture00220810_12400jst


Screencapture00220810_12411jst

 

でも

do shell script "date '+%Y%m%d_%k%M%S'"

Force4u00220930_222435

ってしちゃうのが
一番早いね......笑

|

« [Acrobat]全ファイル全ページ印刷 | トップページ | [Acrobat]メタデータ書き込み...挫折...【解決】 »

AppleScriptBasics」カテゴリの記事

コメント

この記事へのコメントは終了しました。