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*)

|

その他のカテゴリー

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