AppleScript File

フォルダ判定

シンボリックリンク判定が不要なら FinderでOK
シンボリックリンクの判定が必要ならgetResourceValue

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application


on run
  set aliasDefaultLocation to (path to desktop from user domain) as alias
  set strPromptText to "フォルダをえらんでください"
  set strMesText to "フォルダをえらんでください"
  try
    set listFolderPath to (choose folder strMesText with prompt strPromptText default location aliasDefaultLocation with multiple selections allowed, invisibles and showing package contents) as list
  on error
    log "エラーしました"
return "エラーしました"
  end try
open listFolderPath
end run


on open listFolderPath
  ####フォルダの数だけ繰り返し
  repeat with itemFolderPath in listFolderPath
    ######パス フォルダのエイリアス
    set aliasDirPath to itemFolderPath as alias
    ###UNIXパスにして
    set strDirPath to POSIX path of aliasDirPath as text
    ##############################
    ###フォルダ判定 A Finderを使う
    ##############################
    ###シンボリックリンクは判定出来ない(エイリアスのリンク先を参照する)
    ###エイリアス判定もするのでわりと堅実
    tell application "Finder"
      set strKind to (kind of aliasDirPath)
    end tell
    log strKind
    if strKind is not "フォルダ" then
return "処理対象はフォルダのみですA"
    end if
    ##############################
    ###フォルダ判定 B appFileManagerを使う
    ##############################
    ## エイリアス判定はしない(エイリアスのリンク先を参照する)
    ###ファイルマネジャー初期化
    set appFileManager to refMe's NSFileManager's defaultManager()
    set ocidFilePathStr to (refMe's NSString's stringWithString:(strDirPath))
    set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
    set boolDir to (appFileManager's fileExistsAtPath:(ocidFilePath) isDirectory:true)
    if boolDir is false then
return "処理対象はフォルダのみですB"
    end if
    ##############################
    ###フォルダ判定 C getResourceValueを使う
    ##############################
    ## エイリアス判定はしない(エイリアスのリンク先を参照する)
    set ocidFilePathStr to (refMe's NSString's stringWithString:(strDirPath))
    set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
    set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath)
    set listResult to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference))
    set boolIsDir to (item 2 of listResult) as boolean
    log boolIsDir
    if boolDir is false then
return "処理対象はフォルダのみですC"
    end if
    ##############################
    ###フォルダ判定 D getResourceValueを使う
    ##############################
    set ocidFilePathStr to (refMe's NSString's stringWithString:(strDirPath))
    set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
    set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath)
    set listResult to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsAliasFileKey) |error|:(reference))
    set boolIsDir to (item 2 of listResult) as boolean
    log boolIsDir
    if boolIsDir is true then
return "処理対象はフォルダのみです(エイリアス判定)D"
    end if
    
    ##############################
    ###フォルダ判定 E getResourceValueを使う
    ##############################
    set ocidFilePathStr to (refMe's NSString's stringWithString:(strDirPath))
    set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
    set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath)
    set listResult to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsSymbolicLinkKey) |error|:(reference))
    set boolIsDir to (item 2 of listResult) as boolean
    log boolIsDir
    if boolIsDir is true then
return "処理対象はフォルダのみです(シンボリックリンク判定)E"
    end if
    
  end repeat
  
  
  
end open


|

[Basic]ファイルサイズの取得

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#                       com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSURL : a reference to refMe's NSURL
set objFileManager to refMe's NSFileManager's defaultManager()



set ocidUserDesktopPath to (objFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set aliasDefaultLocation to ocidUserDesktopPath as alias

tell application "Finder"
    ##    set aliasDefaultLocation to container of (path to me) as alias
end tell

set listChooseFileUTI to {"public.mp3", "com.apple.m4a-audio"}

set strPromptText to "音楽ファイルを選んでください" as text

set aliasFilePath to (choose file with prompt strPromptText default location (aliasDefaultLocation) of type listChooseFileUTI with invisibles and showing package contents without multiple selections allowed) as alias
-->alias

###パス
set strFilePath to POSIX path of aliasFilePath
set ocidFilePathStr to refNSString's stringWithString:strFilePath
set ocidFilePath to ocidFilePathStr's stringByExpandingTildeInPath()
set ocidFilePathURL to refNSURL's alloc()'s initFileURLWithPath:ocidFilePath isDirectory:false

log doGetFileSize(ocidFilePathURL) as text


log doGetFileSizeFM(ocidFilePathURL) as text



###########################################
### ファイルサイズを読み取るサブ NSURL
###########################################

to doGetFileSize(argURL)
    
    try
        set strClassName to className() of argURL as text
    on error
        set strClassName to class of argURL
    end try
    
    if strClassName is "__NSCFString" then
        set ocidArgFilePath to argURL's stringByExpandingTildeInPath()
        set ocidArgFilePathURL to refNSURL's alloc()'s initFileURLWithPath:ocidArgFilePath
        
    else if strClassName is "NSPathStore2" then
        set ocidArgFilePathURL to refNSURL's alloc()'s initFileURLWithPath:argURL
        
    else if strClassName is "NSURL" then
        set ocidArgFilePathURL to argURL
        
    else if strClassName is alias then
        set strArgFilePath to POSIX path of argURL
        set ocidArgFilePathStr to refNSString's stringWithString:strArgFilePath
        set ocidArgFilePath to ocidArgFilePathStr's stringByExpandingTildeInPath()
        set ocidArgFilePathURL to refNSURL's alloc()'s initFileURLWithPath:ocidArgFilePath
        
    else if strClassName is text then
        set ocidArgFilePathStr to refNSString's stringWithString:argURL
        set ocidArgFilePath to ocidArgFilePathStr's stringByExpandingTildeInPath()
        set ocidArgFilePathURL to refNSURL's alloc()'s initFileURLWithPath:ocidArgFilePath
        
    end if
    ###
    set listResults to ocidArgFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLFileSizeKey) |error|:(reference)
    set strFileSize to (item 2 of listResults) as text
    
    return (strFileSize)
end doGetFileSize


###########################################
### ファイルサイズを読み取るサブ NSFileManager
###########################################

to doGetFileSizeFM(argURL)
    
    try
        set strClassName to className() of argURL as text
    on error
        set strClassName to class of argURL
    end try
    
    if strClassName is "__NSCFString" then
        set ocidArgFilePath to argURL's stringByExpandingTildeInPath()
        
    else if strClassName is "NSPathStore2" then
        set ocidArgFilePath to argURL
        
    else if strClassName is "NSURL" then
        set ocidArgFilePath to argURL's |path|()
        
    else if strClassName is alias then
        set strArgFilePath to POSIX path of argURL
        set ocidArgFilePathStr to refNSString's stringWithString:strArgFilePath
        set ocidArgFilePath to ocidArgFilePathStr's stringByExpandingTildeInPath()
        
    else if strClassName is text then
        set ocidArgFilePathStr to refNSString's stringWithString:argURL
        set ocidArgFilePath to ocidArgFilePathStr's stringByExpandingTildeInPath()
        
        
    end if
    set objFileManager to refMe's NSFileManager's alloc()'s init()
    
    set listAttribute to (objFileManager's attributesOfItemAtPath:ocidArgFilePath |error|:(reference))
    set ocidAttribute to (item 1 of listAttribute)
    
    set ocidFileSize to ocidAttribute's NSFileSize
    
    set strFileSize to ocidFileSize as text
    
    return (strFileSize)
end doGetFileSizeFM

|

[writeToURL]空のテキストファイルを作成する

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSArray : a reference to refMe's NSArray
property refNSString : a reference to refMe's NSString
property refNSURL : a reference to refMe's NSURL
property refNSMutableString : a reference to refMe's NSMutableString


###デスクトップフォルダを求める
set ocidDeskTopDirArray to refMe's NSSearchPathForDirectoriesInDomains(refMe's NSDesktopDirectory, refMe's NSUserDomainMask, true)
log className() of ocidDeskTopDirArray as text
-->(*__NSSingleObjectArrayI*)
set strUserDesktopPath to (ocidDeskTopDirArray's objectAtIndex:0) as text
####ダイアログ用にエイリアス
set aliasUserDesktopPath to POSIX file strUserDesktopPath as alias
###ダイアログ用のテキスト
set strDefaultName to "名称未設定.txt" as text
set strPromptText to "名前を決めてください"
####ファイル名選択ダイアログ(ポイントは:as «class furl»
set aliasPath to (choose file name default location aliasUserDesktopPath default name strDefaultName with prompt strPromptText) as «class furl»
set strPath to POSIX path of aliasPath as text

#####ファイルマネージャー
set objFileManager to refMe's NSFileManager's defaultManager()
####パスを
set ocidNSString to refNSString's stringWithString:strPath
####NSURL
set ocidURLPath to refNSURL's fileURLWithPath:ocidNSString
#####書き込む空のテキスト
set ocidMutableString to refNSMutableString's alloc()'s initWithCapacity:0
####ファイルを書き込む
set boolFileWrite to (ocidMutableString's writeToURL:ocidURLPath atomically:false encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))


|

[stringWithContentsOfURL]テキストファイル読込(テキスト内容がコマンド)

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSMutableArray : a reference to refMe's NSMutableArray
property refNSURL : a reference to refMe's NSURL
property refNSNotFound : a reference to 9.22337203685477E+18 + 5807



####ダイアログで使うデフォルトロケーション
tell application "Finder"
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell
####UTIリスト
set listUTI to {"public.text"}
####ダイアログを出す
set aliasReadFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias

set strReadFilePath to POSIX path of aliasReadFilePath
####ドキュメントのパスをNSString
set ocidReadFilePath to refNSString's stringWithString:strReadFilePath
####ドキュメントのパスをNSURL
set ocidReadFilePathURL to refNSURL's fileURLWithPath:ocidReadFilePath
################
###ファイルの読み込み(結果はリスト形式で戻る)
set listReadDataString to refNSString's stringWithContentsOfURL:ocidReadFilePathURL usedEncoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
###読み込みデータは『ocidReadTextContents』に入る
set ocidReadTextContents to item 1 of listReadDataString
###エラー内容を見る場合
set ocidNSErrorData to item 2 of listReadDataString
if ocidNSErrorData is not (missing value) then
doGetErrorData(ocidNSErrorData)
end if

#######可変Arrayを初期化して準備
set ocidReadData to refNSMutableArray's alloc()'s initWithCapacity:0
#######改行を区切り文字に指定
set ocidNewlineCharacterSett to refMe's NSCharacterSet's newlineCharacterSet()
#######改行で区切りでArrayに格納
set ocidReadData to ocidReadTextContents's componentsSeparatedByCharactersInSet:ocidNewlineCharacterSett

################
####Arrayの回数だけ繰り返し
repeat with ocidDataItem in ocidReadData
log ocidDataItem as text
###各行にあるテキストをコマンドとして
set strLineText to ocidDataItem as text
####コマンド実行
if strLineText is not "" then
try
do shell script strLineText with administrator privileges
end try
end if

end repeat








to doGetErrorData(ocidNSErrorData)
#####個別のエラー情報
log "エラーコード:" & ocidNSErrorData's code() as text
log "エラードメイン:" & ocidNSErrorData's domain() as text
log "Description:" & ocidNSErrorData's localizedDescription() as text
log "FailureReason:" & ocidNSErrorData's localizedFailureReason() as text
log ocidNSErrorData's localizedRecoverySuggestion() as text
log ocidNSErrorData's localizedRecoveryOptions() as text
log ocidNSErrorData's recoveryAttempter() as text
log ocidNSErrorData's helpAnchor() as text
set ocidNSErrorUserInfo to ocidNSErrorData's userInfo()
set ocidAllValues to ocidNSErrorUserInfo's allValues() as list
set ocidAllKeys to ocidNSErrorUserInfo's allKeys() as list
repeat with ocidKeys in ocidAllKeys
if (ocidKeys as text) is "NSUnderlyingError" then
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedDescription() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedFailureReason() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoverySuggestion() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoveryOptions() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s recoveryAttempter() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s helpAnchor() as text
else
####それ以外の値はそのままテキストで読める
log (ocidKeys as text) & ": " & (ocidNSErrorUserInfo's valueForKey:ocidKeys) as text
end if
end repeat

end doGetErrorData

|

[stringWithContentsOfURL]テキストファイル読込

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSURL : a reference to refMe's NSURL
property refNSMutableArray : a reference to refMe's NSMutableArray

###################################
#######入力ファイル関連
####ダイアログで使うデフォルトロケーション
tell application "Finder"
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell
####UTIリスト
set listUTI to {"public.text"}
####ダイアログを出す
set aliasReadFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias

set strReadFilePath to POSIX path of aliasReadFilePath
####ドキュメントのパスをNSString
set ocidReadFilePath to refNSString's stringWithString:strReadFilePath
####ドキュメントのパスをNSURL
set ocidReadFilePathURL to refNSURL's fileURLWithPath:ocidReadFilePath
####ファイル名
set ocidFileName to ocidReadFilePathURL's lastPathComponent()
####拡張子名
set ocidFileExtensionName to ocidReadFilePathURL's pathExtension()
####コンテナ(ファイルのあるディレクトリ)
set ocidContainerDirURL to ocidReadFilePathURL's URLByDeletingLastPathComponent()


###################################
#######ファイルの読み込み(結果はリスト形式で戻る)
set listReadDataString to refNSString's stringWithContentsOfURL:ocidReadFilePathURL usedEncoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
###読み込みデータは『ocidReadTextContents』に入る
set ocidReadTextContents to item 1 of listReadDataString
###エラー内容を見る場合
set ocidNSError to item 2 of listReadDataString
if ocidNSError is not (missing value) then
doGetErrorData(ocidNSError)
end if

###################################
#######読み込んだテキストを改行区切りでリストにする
#######可変Arrayを初期化して準備
set ocidReadData to refNSMutableArray's alloc()'s initWithCapacity:0
#######改行を区切り文字に指定
set ocidNewlineCharacterSett to refMe's NSCharacterSet's newlineCharacterSet()
#######改行で区切りでArrayに格納
set ocidReadData to ocidReadTextContents's componentsSeparatedByCharactersInSet:ocidNewlineCharacterSett


###################################
#######Arrayの回数だけ繰り返し
repeat with ocidReadItem in ocidReadData
###各行にあるテキスト
log ocidReadItem as text



end repeat








to doGetErrorData(ocidNSErrorData)
#####個別のエラー情報
log "エラーコード:" & ocidNSErrorData's code() as text
log "エラードメイン:" & ocidNSErrorData's domain() as text
log "Description:" & ocidNSErrorData's localizedDescription() as text
log "FailureReason:" & ocidNSErrorData's localizedFailureReason() as text
log ocidNSErrorData's localizedRecoverySuggestion() as text
log ocidNSErrorData's localizedRecoveryOptions() as text
log ocidNSErrorData's recoveryAttempter() as text
log ocidNSErrorData's helpAnchor() as text
set ocidNSErrorUserInfo to ocidNSErrorData's userInfo()
set ocidAllValues to ocidNSErrorUserInfo's allValues() as list
set ocidAllKeys to ocidNSErrorUserInfo's allKeys() as list
repeat with ocidKeys in ocidAllKeys
if (ocidKeys as text) is "NSUnderlyingError" then
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedDescription() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedFailureReason() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoverySuggestion() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoveryOptions() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s recoveryAttempter() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s helpAnchor() as text
else
####それ以外の値はそのままテキストで読める
log (ocidKeys as text) & ": " & (ocidNSErrorUserInfo's valueForKey:ocidKeys) as text
end if
end repeat

end doGetErrorData

|

[NSDATA]ファイルダウンロード

テキストファイルにあるURLを順番にダウンロード

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSMutableArray : a reference to refMe's NSMutableArray
property refNSURL : a reference to refMe's NSURL
property refNSData : a reference to refMe's NSData

property refNSDate : a reference to refMe's NSDate
property refNSDateFormatter : a reference to NSDateFormatter
property refNSURLComponents : a reference to refMe's NSURLComponents
property refNSNotFound : a reference to 9.22337203685477E+18 + 5807

set strBaseURL to ""

####ダイアログで使うデフォルトロケーション
tell application "Finder"
set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell
####UTIリスト
set listUTI to {"public.text"}
####ダイアログを出す
set aliasReadFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias

set strReadFilePath to POSIX path of aliasReadFilePath
####ドキュメントのパスをNSString
set ocidReadFilePath to refNSString's stringWithString:strReadFilePath
####ドキュメントのパスをNSURL
set ocidReadFilePathURL to refNSURL's fileURLWithPath:ocidReadFilePath
################
######保存先
###日付時間をフォルダ名
set strDateAndTime to doGetDateNo("yyyyMMdd_hhmmss") as text
###フォルダ作成パス
set strSaveDirPath to ("~/Downloads/" & strDateAndTime & "") as text
###NSString
set ocidSaveDirPath to (refNSString's stringWithString:strSaveDirPath)
###フルパス
set ocidSaveDirFullPath to ocidSaveDirPath's stringByStandardizingPath
###NSURL
set ocidSaveDirFullPathURL to (refNSURL's alloc()'s initFileURLWithPath:ocidSaveDirFullPath)
###作るフォルダの属性
(*
###主要なモード NSFilePosixPermissions
777-->511
775-->509
770-->504
755-->493
750-->488
700-->448
555-->365
333-->219
#####NSFileGroupOwnerAccountID
ゲストのGID
201-->_guest
99-->_unknown
-2-->nobody
*)
###ファイルマネージャー初期化
set objFileManager to refMe's NSFileManager's defaultManager()
###保存先フォルダ作成
set boolMakeNewFolder to (objFileManager's createDirectoryAtURL:ocidSaveDirFullPathURL withIntermediateDirectories:true attributes:({NSFilePosixPermissions:511}) |error|:(reference))

################
###ファイルの読み込み(結果はリスト形式で戻る)
set listReadDataString to refNSString's stringWithContentsOfURL:ocidReadFilePathURL usedEncoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
###読み込みデータは『ocidReadTextContents』に入る
set ocidReadTextContents to item 1 of listReadDataString
#######可変Arrayを初期化して準備
set ocidReadData to refNSMutableArray's alloc()'s initWithCapacity:0
#######改行を区切り文字に指定
set ocidNewlineCharacterSett to refMe's NSCharacterSet's newlineCharacterSet()
#######改行で区切りでArrayに格納
set ocidReadData to ocidReadTextContents's componentsSeparatedByCharactersInSet:ocidNewlineCharacterSett

################
####Arrayの回数だけ繰り返し
repeat with ocidDataItem in ocidReadData
log ocidDataItem as text
###各行にあるテキストをURLとして
set ocidDownLoadURL to (refNSURL's URLWithString:ocidDataItem)
###ファイル名取得
set ocidFileName to ocidDownLoadURL's lastPathComponent() as text
###保存ファイルのURL
set ocidSaveFilePathURL to (ocidSaveDirFullPathURL's URLByAppendingPathComponent:ocidFileName)
log ocidSaveFilePathURL as text
###ファイルをダウンロード
set listDownLoadData to (refNSData's dataWithContentsOfURL:ocidDownLoadURL options:0 |error|:(reference))
set ocidDownLoadData to (item 1 of listDownLoadData)
try
log className() of (item 1 of listDownLoadData) as text
log item 2 of listDownLoadData
end try
###ファイル保存
set boolSaveFileDone to (ocidDownLoadData's writeToURL:ocidSaveFilePathURL atomically:true)
####リリースするとクラッシュする事があるので通常初期化
set ocidDownLoadURL to ""
set ocidDownLoadData to ""
end repeat


to doGetDateNo(strDateFormat)
####日付情報の取得
set ocidDate to current application's NSDate's |date|()
###日付のフォーマットを定義
set ocidNSDateFormatter to current application's NSDateFormatter's alloc()'s init()
ocidNSDateFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
ocidNSDateFormatter's setDateFormat:strDateFormat
set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate
set strDateAndTime to ocidDateAndTime as text
return strDateAndTime
end doGetDateNo

|

[テキストデータ]テキスト・データの保存

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSURL : a reference to refMe's NSURL


set strTextData to "美しい日本語" as text
set ocidTextData to refNSString's stringWithString:strTextData

################
###ファイルパス
set strFilePath to "~/Desktop/read.txt"

set ocidRelativePath to refNSString's stringWithString:strFilePath
set ocidFullPath to ocidRelativePath's stringByStandardizingPath
set ocidSaveFilePathURL to refNSURL's alloc()'s initFileURLWithPath:ocidFullPath



##############
###ファイル書き込み
set listSaveFileDone to ocidTextData's writeToURL:ocidSaveFilePathURL atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)

###読み込みデータは『ocidReadTextContents』に入る
set boolSaveFileDone to item 1 of listSaveFileDone

###エラー内容を見る場合
if boolSaveFileDone is false then
set ocidNSError to item 2 of listSaveFileDone
doGetDateNo(ocidNSError)
end if
to doGetDateNo(ocidNSErrorData)
#####個別のエラー情報
log "エラーコード:" & ocidNSErrorData's code() as text
log "エラードメイン:" & ocidNSErrorData's domain() as text
log "Description:" & ocidNSErrorData's localizedDescription() as text
log "FailureReason:" & ocidNSErrorData's localizedFailureReason() as text
log ocidNSErrorData's localizedRecoverySuggestion() as text
log ocidNSErrorData's localizedRecoveryOptions() as text
log ocidNSErrorData's recoveryAttempter() as text
log ocidNSErrorData's helpAnchor() as text
set ocidNSErrorUserInfo to ocidNSErrorData's userInfo()
set ocidAllValues to ocidNSErrorUserInfo's allValues() as list
set ocidAllKeys to ocidNSErrorUserInfo's allKeys() as list
repeat with ocidKeys in ocidAllKeys
if (ocidKeys as text) is "NSUnderlyingError" then
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedDescription() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedFailureReason() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoverySuggestion() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoveryOptions() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s recoveryAttempter() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s helpAnchor() as text
else
####それ以外の値はそのままテキストで読める
log (ocidKeys as text) & ": " & (ocidNSErrorUserInfo's valueForKey:ocidKeys) as text
end if
end repeat

end doGetDateNo

|

[テキストデータ]テキスト・ファイルの読み込み

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSURL : a reference to refMe's NSURL

################
###ファイルパス
set strFilePath to "~/Desktop/read.txt"

set ocidRelativePath to refNSString's stringWithString:strFilePath
set ocidFullPath to ocidRelativePath's stringByStandardizingPath
set ocidFilePathURL to refNSURL's alloc()'s initFileURLWithPath:ocidFullPath

set ocidFileName to ocidFilePathURL's lastPathComponent()
set ocidFileExtensionName to ocidFilePathURL's pathExtension()
set ocidContainerDirURL to ocidFilePathURL's URLByDeletingLastPathComponent()


################
###ファイルの読み込み(結果はリスト形式で戻る)
set listReadDataString to refNSString's stringWithContentsOfURL:ocidFilePathURL usedEncoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
###読み込みデータは『ocidReadTextContents』に入る
set ocidReadTextContents to item 1 of listReadDataString
###エラー内容を見る場合
set ocidNSError to item 2 of listReadDataString
if ocidNSError is not (missing value) then
doGetErrorData(ocidNSError)
end if



################
####Arrayの回数だけ繰り返し
repeat with ocidReadTextItem in ocidReadTextContents
log ocidReadTextItem as text
###各行にあるテキストをURLとして
set ocidDownLoadURL to (refNSURL's URLWithString:ocidReadTextItem)


end repeat








to doGetErrorData(ocidNSErrorData)
#####個別のエラー情報
log "エラーコード:" & ocidNSErrorData's code() as text
log "エラードメイン:" & ocidNSErrorData's domain() as text
log "Description:" & ocidNSErrorData's localizedDescription() as text
log "FailureReason:" & ocidNSErrorData's localizedFailureReason() as text
log ocidNSErrorData's localizedRecoverySuggestion() as text
log ocidNSErrorData's localizedRecoveryOptions() as text
log ocidNSErrorData's recoveryAttempter() as text
log ocidNSErrorData's helpAnchor() as text
set ocidNSErrorUserInfo to ocidNSErrorData's userInfo()
set ocidAllValues to ocidNSErrorUserInfo's allValues() as list
set ocidAllKeys to ocidNSErrorUserInfo's allKeys() as list
repeat with ocidKeys in ocidAllKeys
if (ocidKeys as text) is "NSUnderlyingError" then
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedDescription() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedFailureReason() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoverySuggestion() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s localizedRecoveryOptions() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s recoveryAttempter() as text
log (ocidNSErrorUserInfo's valueForKey:ocidKeys)'s helpAnchor() as text
else
####それ以外の値はそのままテキストで読める
log (ocidKeys as text) & ": " & (ocidNSErrorUserInfo's valueForKey:ocidKeys) as text
end if
end repeat

end doGetErrorData

|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom