AppleScript NSURL File

[AppleScript]ファイル名を取得する

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#error number -128
#
# 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 aliasDefaultLocation to (path to desktop folder from user domain) as alias

####UTIリスト PDFのみ
set listUTI to {"public.item"}

####ダイアログを出す
set aliasFilePath to (choose file with prompt "ファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias

tell application "Finder"
set objInfo to info for aliasFilePath
####ファイル名取得
set strFileName to name of objInfo as text
log strFileName
-->ファイル名
end tell

set strFilePath to POSIX path of aliasFilePath
####ドキュメントのパスをNSString
set ocidFilePath to refNSString's stringWithString:strFilePath
####ドキュメントのパスをNSURL
set ocidNSUrlPath to refNSURL's fileURLWithPath:ocidFilePath
#####ファイル名取得
set ocidFileName to ocidNSUrlPath's lastPathComponent()
####ocid形式
log ocidFileName
-->ファイル名
log className() of ocidFileName as text
(*NSPathStore2*)

###テキスト形式
set strFileName to ocidFileName as text
log strFileName
-->ファイル名
log class of strFileName
(*text*)

|

[stringWithContentsOfURL]URLデータを取得(テキスト)

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos13なので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 refNSData : a reference to refMe's NSData
property refNSMutableString : a reference to refMe's NSMutableString
property refNSMutableArray : a reference to refMe's NSMutableArray
property refNSPasteboard : a reference to refMe's NSPasteboard
set ocidPasteboard to refNSPasteboard's generalPasteboard()

###クリックボードの中のURLを取得
set ocidPasteboardTypeString to ocidPasteboard's stringForType:(refMe's NSPasteboardTypeString)

##可変テキスト形式で格納
set ocidCurlURL to refNSMutableString's alloc()'s initWithCapacity:0
ocidCurlURL's setString:ocidPasteboardTypeString

if (ocidCurlURL as text) starts with "curl" then
log "処理開始"
else
return "CURLとしてコピーしてください"
end if

####################################
###URLの処理
###改行を取る
set ocidCurlURLLength to ocidCurlURL's |length|()
set ocidCurlURLRange to {location:0, |length|:ocidCurlURLLength}
ocidCurlURL's replaceOccurrencesOfString:("\n") withString:("") options:(refMe's NSRegularExpressionSearch) range:ocidCurlURLRange

###'』でリスト化して2番目がURL
set ocidDelimiters to (refMe's NSCharacterSet)'s characterSetWithCharactersInString:"'"
set ocidCurlURLArray to ocidCurlURL's componentsSeparatedByCharactersInSet:ocidDelimiters

###URL確定(クエリー入り)
set ocidURLString to ocidCurlURLArray's objectAtIndex:1
set ocidURL to refNSURL's URLWithString:ocidURLString

###URL部分(クエリー無し)
set ocidURLComponents to (refMe's NSURLComponents)'s componentsWithURL:ocidURL resolvingAgainstBaseURL:true
ocidURLComponents's setQueryItems:(missing value)
set ocidBaseURL to ocidURLComponents's |URL|

###クエリー部分
set ocidQueryName to ocidURL's query()

###ヘッダー部
set ocidDelimiters to (refMe's NSCharacterSet)'s characterSetWithCharactersInString:"\\"
set ocidHeaderArray to ocidCurlURL's componentsSeparatedByCharactersInSet:ocidDelimiters
set ocidHeaderArrayLength to ocidHeaderArray's |count|()
set ocidHeaderRange to refMe's NSMakeRange(1, (ocidHeaderArrayLength - 1))
set ocidSubbArray to ocidHeaderArray's subarrayWithRange:ocidHeaderRange
set ocidHeaderText to (ocidSubbArray's componentsJoinedByString:"")

log ocidURL's absoluteString() as text
log ocidBaseURL's absoluteString() as text
log ocidQueryName as text
log ocidHeaderText as text

####################################
###URLからデータを取得

set listDownLoadData to refNSString's stringWithContentsOfURL:ocidURL encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
-->戻り値がList
####データ部
set ocidGetDataString to (item 1 of listDownLoadData)
####エラー情報(不要な場合は削除可)
set ocidNSErrorData to item 2 of listDownLoadData
if ocidNSErrorData is not (missing value) then
doGetErrorData(ocidNSErrorData)
return "エラー"
end if










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
####それ以外の値はそのままテキストで読める
try
log (ocidKeys as text) & ": " & (ocidNSErrorUserInfo's valueForKey:ocidKeys) as text
end try
end if
end repeat

end doGetErrorData

|

[NSURL]基礎ふたたび

結果は同じだけど、相対パスから絶対パスに展開するのには
stringByStandardizingPathを使うのが正解



#!/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"
set ocidRelativePath to refNSString's stringWithString:strFilePath
-->(*~/Desktop*)(*__NSCFString*)
set ocidFullPath to ocidRelativePath's stringByStandardizingPath
-->(*/Users/ユーザー名/Desktop*)(*NSPathStore2*)
set ocidDefaultLocation to refNSURL's alloc()'s initFileURLWithPath:ocidFullPath
-->(*Macintosh HD:Users:ユーザー名:Desktop:*)(*NSURL*)
set aliasDefaultLocation to ocidDefaultLocation as alias

set listChooseFile to (choose file with prompt "ファイルを選んでください" default location aliasDefaultLocation of type {"public.item"} with invisibles and multiple selections allowed without showing package contents) as list

repeat with objFile in listChooseFile
set strFilePath to POSIX path of objFile as text
tell application "Finder"
set objInfo to info for objFile
set strFileName to name of objInfo as text
set strExeName to name extension of objInfo as text
set aliasContainerDir to container of objFile as alias
end tell

set ocidFilePathURL to (refNSURL's alloc()'s initFileURLWithPath:strFilePath)
-->(*NSURL*)
set ocidFileName to ocidFilePathURL's lastPathComponent()
-->ファイル名(*NSPathStore2*)
set ocidFileExtensionName to ocidFilePathURL's pathExtension()
-->拡張子(*NSPathStore2*)
set strFileSystemRep to ocidFilePathURL's fileSystemRepresentation()
-->テキスト形式のPOSIX PATH(*text*)
set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
-->URLのひとつ上の階層-->URLのディレクトリ(*NSURL*)
set ocidBaseFilePathURL to ocidFilePathURL's URLByDeletingPathExtension()
-->拡張子を取ったベースファイル名のURL


set ocidPath to ocidFilePathURL's |path|()
-->(*__NSCFString*) POSIX PATH 形式
set ocidAbsoluteStringPath to ocidFilePathURL's absoluteString()
-->RFC 形式(*__NSCFString*)
set ocidAbsoluteURL to ocidFilePathURL's absoluteURL()
-->(*NSURL*)
set ocidRelativeStringPath to ocidFilePathURL's relativeString()
-->RFC 形式(*__NSCFString*)
set ocidRelativePath to ocidFilePathURL's relativePath()
-->POSIX PATH(*__NSCFString*)
set ocidResourceSpecifier to ocidFilePathURL's resourceSpecifier()
-->:以降を%エンコードで(*__NSCFString*)
set ocidFileReferenceURL to ocidFilePathURL's fileReferenceURL()
-->(*NSURL*)
set ocidNewFilePathURL to ocidFilePathURL's URLByStandardizingPath()
-->(*NSURL*)
set ocidFilePathURL to ocidFilePathURL's filePathURL()
-->(*NSURL*)



end repeat



set strRelativePath to ("~/Desktop/") as text
(*~/Desktop/*)
set ocidRelativePath to refNSString's stringWithString:strRelativePath
(*~/Desktop/*)
(*__NSCFString*)
set ocidFullPath to ocidRelativePath's stringByExpandingTildeInPath()
(*/Users/ユーザー名/Desktop*)
(*NSPathStore2*)
set ocidFullPath to ocidRelativePath's stringByStandardizingPath()
(*/Users/ユーザー名/Desktop*)
(*NSPathStore2*)


#!/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 strRelativePath to ("~/Desktop/") as text
(*~/Desktop/*)
set ocidRelativePath to refNSString's stringWithString:strRelativePath
(*__NSCFString*)
set ocidFullPath to ocidRelativePath's stringByStandardizingPath()
(*/Users/ユーザー名/Desktop*)
(*NSPathStore2*)

set ocidFullPathURL to refMe's NSURL's fileURLWithPath:ocidFullPath
log ocidFullPathURL as text
log className() of ocidFullPathURL as text
(*Macintosh HD:Users:ユーザー名:Desktop:*)
(*NSURL*)
log ocidFullPathURL's |path|() as text
(*/Users/ユーザー名/Desktop*)
log ocidFullPathURL's absoluteString() as text
(*file:///Users/ユーザー名/Desktop/*)
log ocidFullPathURL as text
(*Macintosh HD:Users:ユーザー名:Desktop:*)
log ocidFullPathURL as alias
(*alias Macintosh HD:Users:ユーザー名:Desktop:*)

|

[filePathURL]alias形式リードオンリー

#!/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/")
###NSStringにして
set ocidPosixPath to refNSString's stringWithString:strFilePath
##NSStringフルパスにして
set strBaseFilePath to ocidPosixPath's stringByStandardizingPath

set ocidBaseFilePath to refMe's NSURL's fileURLWithPath:strBaseFilePath
log ocidBaseFilePath as text
(*Macintosh HD:Users:ユーザー名:Desktop:*)
log className() of ocidBaseFilePath as text
(*NSURL*)




set strRelativePath to "名称未設定" as text
###NSStringにして
set ocidRelativePath to refNSString's stringWithString:strRelativePath
log ocidRelativePath as text
(*名称未設定*)
log className() of ocidRelativePath as text
(*__NSCFString*)
####パスに使える文字列を定義
set ocidNSCFCharacterSet to refMe's NSCharacterSet's URLPathAllowedCharacterSet()
####パスに使える文字列に変換
set ocidEncodedString to ocidRelativePath's stringByAddingPercentEncodingWithAllowedCharacters:ocidNSCFCharacterSet
log ocidEncodedString as text
(*%E5%90%8D%E7%A7%B0%E6%9C%AA%E8%A8%AD%E5%AE%9A*)
log className() of ocidEncodedString as text
(*__NSCFString*)



set ocidNSUrlPath to refNSURL's alloc()'s initWithString:ocidEncodedString relativeToURL:ocidBaseFilePath
log ocidNSUrlPath's |path|() as text
(*/Users/ユーザー名/Desktop/名称未設定*)
log className() of ocidNSUrlPath as text
(*NSURL*)


set ocidPathURL to ocidNSUrlPath's filePathURL()
log ocidPathURL as text
(*Macintosh HD:Users:ユーザー名:Desktop:名称未設定*)
log className() of ocidPathURL as text
(*NSURL*)

|

[fileReferenceURL]alias形式 参照先

#!/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 ("/tmp")
###NSStringにして
set ocidPosixPath to refNSString's stringWithString:strFilePath


#############################################

set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:ocidPosixPath
log ocidFilePathURL's |path|() as text
(*/tmp*)
log ocidFilePathURL's absoluteString() as text
(*file:///tmp/*)
log ocidFilePathURL as alias
(*alias Macintosh HD:private:tmp:*)
log className() of ocidFilePathURL as text
(*NSURL*)

####参照先
set ocidReferenceURL to ocidFilePathURL's fileReferenceURL()
log ocidReferenceURL as text
(*Macintosh HD:private:tmp*)
log className() of ocidReferenceURL as text
(*NSURL*)

####シンボリックリンクか?どうか?
set boolIsFileReferenceURL to ocidFilePathURL's isFileReferenceURL()
log boolIsFileReferenceURL
if boolIsFileReferenceURL is false then
log "パスはエイリアスです"
else if boolIsFileReferenceURL is true then
log "パスはオリジナルです"
end if



#!/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/")
###NSStringにして
set ocidPosixPath to refNSString's stringWithString:strFilePath
##NSStringフルパスにして
set strBaseFilePath to ocidPosixPath's stringByStandardizingPath

set ocidBaseFilePath to refMe's NSURL's fileURLWithPath:strBaseFilePath
log ocidBaseFilePath as text
(*Macintosh HD:Users:ユーザー名:Desktop:*)
log className() of ocidBaseFilePath as text
(*NSURL*)


set strRelativePath to "名称未設定フォルダ" as text
###NSStringにして
set ocidRelativePath to refNSString's stringWithString:strRelativePath
log ocidRelativePath as text
(*名称未設定*)
log className() of ocidRelativePath as text
(*__NSCFString*)
####パスに使える文字列を定義
set ocidNSCFCharacterSet to refMe's NSCharacterSet's URLPathAllowedCharacterSet()
####パスに使える文字列に変換
set ocidEncodedString to ocidRelativePath's stringByAddingPercentEncodingWithAllowedCharacters:ocidNSCFCharacterSet
log ocidEncodedString as text
(*%E5%90%8D%E7%A7%B0%E6%9C%AA%E8%A8%AD%E5%AE%9A*)
log className() of ocidEncodedString as text
(*__NSCFString*)


set ocidNSUrlPath to refNSURL's alloc()'s initWithString:ocidEncodedString relativeToURL:ocidBaseFilePath
log ocidNSUrlPath's |path|() as text
(*/Users/ユーザー名/Desktop/名称未設定フォルダ*)
log className() of ocidNSUrlPath as text
(*NSURL*)

set ocidReference to ocidNSUrlPath's fileReferenceURL()
log ocidReference as text
(*Macintosh HD:Users:ユーザー名:Desktop:名称未設定フォルダ:*)
if ocidReference is (missing value) then
log "そのパスは実在しません"
else
log ocidReference as text
(*Macintosh HD:Users:ユーザー名:Desktop:名称未設定フォルダ:*)
log className() of ocidReference as text
(*NSURL*)
end if

|

[initWithString]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 refNSURL : a reference to refMe's NSURL


#############################################

set strBaseFilePath to "/Library/Documentation/"


set ocidNSUrlPath to refMe's NSURL's alloc()'s initWithString:strBaseFilePath
log ocidNSUrlPath's |path|() as text
log className() of ocidNSUrlPath as text


set ocidBaseFilePath to refMe's NSURL's fileURLWithPath:strBaseFilePath
log ocidBaseFilePath as text
(*Macintosh HD:Library:Documentation:*)
log className() of ocidBaseFilePath as text
(*NSURL*)

set strRelativePath to "名称未設定フォルダ" as text
###NSStringにして
set ocidRelativePath to refNSString's stringWithString:strRelativePath
log ocidRelativePath as text
(*名称未設定*)
log className() of ocidRelativePath as text
(*__NSCFString*)
####パスに使える文字列を定義
set ocidNSCFCharacterSet to refMe's NSCharacterSet's URLPathAllowedCharacterSet()
####パスに使える文字列に変換
set ocidEncodedString to ocidRelativePath's stringByAddingPercentEncodingWithAllowedCharacters:ocidNSCFCharacterSet
log ocidEncodedString as text
(*%E5%90%8D%E7%A7%B0%E6%9C%AA%E8%A8%AD%E5%AE%9A*)
log className() of ocidEncodedString as text
(*__NSCFString*)



set ocidNSUrlPath to refNSURL's alloc()'s initWithString:ocidEncodedString relativeToURL:ocidBaseFilePath
log ocidNSUrlPath's |path|() as text
(*/Library/Documentation/名称未設定フォルダ*)
log className() of ocidNSUrlPath as text
(*NSURL*)

|

[initFileURLWithPath:relativeToURL:]ベースURLとパスアイテム

WithPath
同じディレクトリ(ベースURL)にある複数の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 refNSURL : a reference to refMe's NSURL


#############################################

###デスクトップ
set strFilePath to ("~/Desktop/")
###NSStringにして
set ocidPosixPath to refNSString's stringWithString:strFilePath
##NSStringフルパスにして
set strBaseFilePath to ocidPosixPath's stringByStandardizingPath
log strBaseFilePath as text
(*/Users/ユーザー名/Desktop*)
log className() of strBaseFilePath as text
(*NSPathStore2*)

#############################################

set ocidFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:strBaseFilePath
log ocidFilePathURL's |path|() as text
(*/Users/ユーザー名/Desktop*)
log ocidFilePathURL's absoluteString() as text
(*file:///Users/ユーザー名/Desktop/*)
log ocidFilePathURL as alias
(*Macintosh HD:Users:ユーザー名:Desktop:*)
log className() of ocidFilePathURL as text
(*NSURL*)

#############################################
set strRelativePath to "名称未設定フォルダ"

###NSStringにして
set ocidRelativePath to refNSString's stringWithString:strRelativePath
log ocidRelativePath as text
(*名称未設定フォルダ*)
log className() of ocidRelativePath as text
(*__NSCFString*)
####パスに使える文字列を定義
set ocidNSCFCharacterSet to refMe's NSCharacterSet's URLPathAllowedCharacterSet()
####パスに使える文字列に変換
set ocidEncodedURL to ocidRelativePath's stringByAddingPercentEncodingWithAllowedCharacters:ocidNSCFCharacterSet
log ocidEncodedURL as text
(*%E5%90%8D%E7%A7%B0%E6%9C%AA%E8%A8%AD%E5%AE%9A*)
log className() of ocidEncodedURL as text
(*__NSCFString*)


set ocidRelativePathURL to refNSURL's alloc()'s initFileURLWithPath:ocidEncodedURL relativeToURL:ocidFilePathURL
log ocidRelativePathURL's |path|() as text
(*/Users/ユーザー名/Desktop/%E5%90%8D%E7%A7%B0%E6%9C%…*)
log ocidRelativePathURL's |path|() as text
(*/Users/ユーザー名/Desktop/%E5%90%8D%E7%A7%B0%E6%9C%…*)
log ocidRelativePathURL's absoluteString() as text
(*file:///Users/ユーザー名/Desktop/%E5%90%8D%E7%A7%B0%E6%9C%…*)
log ocidRelativePathURL as text
(*Macintosh HD:Users:ユーザー名:Desktop:%E5%90%8D%E7%A7%B0%E6%9C%…*)
log className() of ocidRelativePathURL as text
(*NSURL*)


set ocidRelativePathBaseURL to ocidRelativePathURL's baseURL()
log ocidRelativePathBaseURL's |path|() as text
(*/Users/ユーザー名/Desktop*)
log ocidRelativePathBaseURL's absoluteString() as text
(*file:///Users/ユーザー名/Desktop/*)
log ocidRelativePathBaseURL as alias
(*Macintosh HD:Users:ユーザー名:Desktop:*)
log className() of ocidRelativePathBaseURL as text
(*NSURL*)

|

[initFileURLWithPath:isDirectory:]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 refNSURL : a reference to refMe's NSURL

#############################################

set strBaseFilePath to "/Library/Documentation/"

set ocidBaseFilePath to refMe's NSURL's fileURLWithPath:strBaseFilePath isDirectory:true
log ocidBaseFilePath as text
(*Macintosh HD:Library:Documentation:*)
log className() of ocidBaseFilePath as text
(*NSURL*)

set ocidNSUrlPath to refMe's NSURL's alloc()'s initFileURLWithPath:strFilePath isDirectory:true
log ocidNSUrlPath as text
(*Macintosh HD:Library:Documentation:*)
log className() of ocidNSUrlPath as text
(*NSURL*)

|

[URLWithString:relativeToURL:]ベースURLにパスアイテム

同じディレクトリ(ベースURL)にある複数の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 refNSURL : a reference to refMe's NSURL

#############################################


set strBaseFilePath to "/Library/Documentation/"

set ocidBaseFilePath to refMe's NSURL's fileURLWithPath:strBaseFilePath isDirectory:true
log ocidBaseFilePath as text
(*Macintosh HD:Library:Documentation:*)
log className() of ocidBaseFilePath as text
(*NSURL*)


set strRelativePath to "名称未設定" as text
###NSStringにして
set ocidRelativePath to refNSString's stringWithString:strRelativePath
log ocidRelativePath as text
(*名称未設定*)
log className() of ocidRelativePath as text
(*__NSCFString*)
####パスに使える文字列を定義
set ocidNSCFCharacterSet to refMe's NSCharacterSet's URLPathAllowedCharacterSet()
####パスに使える文字列に変換
set ocidEncodedURL to ocidRelativePath's stringByAddingPercentEncodingWithAllowedCharacters:ocidNSCFCharacterSet
log ocidEncodedURL as text
(*%E5%90%8D%E7%A7%B0%E6%9C%AA%E8%A8%AD%E5%AE%9A*)
log className() of ocidEncodedURL as text
(*__NSCFString*)

set ocidNSUrlPath to refMe's NSURL's alloc()'s initFileURLWithPath:ocidEncodedURL isDirectory:false relativeToURL:ocidBaseFilePath
log ocidNSUrlPath as text
(*Macintosh HD:Library:Documentation:%E5%90%8D%E7%A7%B0%E6%9C%AA%E8%A8%AD%E5%AE%9A*)
log className() of ocidNSUrlPath as text
(*NSURL*)
####baseURLを認識する
log ocidNSUrlPath's baseURL() as text
(*Macintosh HD:Library:Documentation:*)
log (className() of (ocidNSUrlPath's baseURL())) as text
(*NSURL*)

|

[initFileURLWithPath]NSURLの初期化

#!/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 strBaseFilePath to "/Library/Documentation/"

set ocidBaseFilePath to refMe's NSURL's fileURLWithPath:strBaseFilePath
log ocidBaseFilePath as text
(*Macintosh HD:Library:Documentation:*)
log className() of ocidBaseFilePath as text
(*NSURL*)


set ocidBaseFilePath to refMe's NSURL's alloc()'s initFileURLWithPath:strBaseFilePath
log ocidBaseFilePath as text
(*Macintosh HD:Library:Documentation:*)
log className() of ocidBaseFilePath as text
(*NSURL*)

|

その他のカテゴリー

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