20260522

[AppleScript]英語環境でのcurrent dateでの戻り値の処理

[AppleScript]英語環境でのcurrent dateでの戻り値の処理

NOTE記事一覧ですnote.com

【スクリプトエディタで開く】 |

current date.scpt.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
004(*
005英語環境用です
006日本語環境では、current dateの戻り値が異なるため使えません
007
008
009
010com.cocolog-nifty.quicktimer.icefloe *)
011----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
012use AppleScript version "2.8"
013use scripting additions
014
015set aliasPrefDir to (path to preferences from user domain) as alias
016tell application "Finder"
017   set aliasGlobalPreferences to (file ".GlobalPreferences.plist" of folder aliasPrefDir) as alias
018end tell
019set strGlobalPreferences to (POSIX path of aliasGlobalPreferences) as string
020
021tell application "System Events"
022   tell property list file strGlobalPreferences
023      try
024         set bool24H to (value of property list item "AppleICUForce24HourTime") as boolean
025      on error strErrMsg number numErrNo
026         set bool24H to true as boolean
027      end try
028   end tell
029end tell
030
031set strDate to (current date) as string
032
033if bool24H is false then
034   #NARROW NO-BREAK SPACE  U+202F
035   set strNarrowNoBreakSpace to (character id 8239) as string
036   #SPACE U+0020 
037   set strSpace to (character id 32) as string
038   #区切り文字でリストにして List it with a separed character
039   set strDelim to AppleScript's text item delimiters
040   set AppleScript's text item delimiters to strNarrowNoBreakSpace
041   set listDate to (every text item of strDate) as list
042   #AMPMはここで取得
043   set strMeridiem to (last item of listDate) as string
044   #スペースで置換 Replace with space
045   set AppleScript's text item delimiters to strSpace
046   set strDate to listDate as string
047   set listDate to (every text item of strDate) as list
048   set strTime to (item ((count of listDate) - 1) of listDate) as string
049   set AppleScript's text item delimiters to strDelim
050else if bool24H is true then
051   set strDelim to AppleScript's text item delimiters
052   set AppleScript's text item delimiters to space
053   set listDate to (every text item of strDate) as list
054   set strTime to (last item of listDate) as string
055   set AppleScript's text item delimiters to ":"
056   set listTime to (every text item of strTime) as list
057   set numH to (first item of listTime) as integer
058   if numH12 then
059      set strMeridiem to ("PM") as string
060      set strH to (numH - 12) as string
061      copy strH to first item of listTime
062   else
063      set strMeridiem to ("AM") as string
064   end if
065   set strTime to listTime as string
066   set AppleScript's text item delimiters to strDelim
067end if
068
069return (strMeridiem & " " & strTime) as string
AppleScriptで生成しました