[AppleScript・OBJC]ファイル名を取得する
ソース | |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* |
004 | ファイルパスからのファイル名の取得 |
005 | 【A: text】:path:UNIX形式のパス 実態参照しない posix path 形式 |
006 | 【B: alias】:alias:エイリアス形式のパス 実態参照するエイリアス形式 |
007 | 【C: furl】:frul:実態参照しないエイリアス形式 |
008 | 【D: URL】:URL:URL形式 |
009 | 【E: specifier】:HFS:Finder file形式 |
010 | |
011 | #【F:NSString】OBJC NSString=テキスト |
012 | #【G:NSPathStore2】OBJC NSPathStore2=パス(テキスト) |
013 | #【H:NSURL】OBJC NSURL |
014 | |
015 | 基本は同じ |
016 | UNIXパス形式に変換してからNSStringで取得するか? |
017 | NSURLにしてから取得するか? |
018 | そして |
019 | ファイルパスの場合『は』lastPathComponentがファイル名 |
020 | |
021 | 注意が必要なのは1点 |
022 | UNIXパスで『~』が使われている場合 |
023 | NSStringでは意図した値にならない事があるので |
024 | NSPathStore2に展開して絶対パスにする必要がある場合がある |
025 | |
026 | com.cocolog-nifty.quicktimer.icefloe *) |
027 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
028 | ##自分環境がos12なので2.8にしているだけです |
029 | use AppleScript version "2.8" |
030 | use framework "Foundation" |
031 | use scripting additions |
032 | property refMe : a reference to current application |
033 | |
034 | #################################### |
035 | set strFilePath to ("/Library/Documentation/Acknowledgements.rtf") as text |
036 | |
037 | #OBJC内でのパス形式 |
038 | #【F:NSString】OBJC NSString=テキスト |
039 | set ocidFilePathStr to current application's NSString's stringWithString:(strFilePath) |
040 | |
041 | #【G:NSPathStore2】OBJC NSPathStore2=パス(テキスト) |
042 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
043 | |
044 | #【H:NSURL】OBJC NSURL |
045 | set ocidFilePathURL to current application's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false) |
046 | |
047 | #################################### |
048 | #AS内でのパスの入力形式 |
049 | #【A: text】:path:UNIX形式のパス 実態参照しない posix path 形式 |
050 | set strFilePath to ("/Library/Documentation/Acknowledgements.rtf") as text |
051 | |
052 | #【B: alias】:alias:エイリアス形式のパス 実態参照するエイリアス形式 |
053 | set aliasFilePath to ("Macintosh HD:Library:Documentation:Acknowledgements.rtf") as alias |
054 | |
055 | #【C: furl】:frul:実態参照しないエイリアス形式 |
056 | set aliasFilePath to ("Macintosh HD:Library:Documentation:Acknowledgements.rtf") as «class furl» |
057 | |
058 | #【D: URL】:URL:URL形式 |
059 | #このClassはscripting additionsが必要になります |
060 | set urlFilePath to ("file:///Library/Documentation/Acknowledgements.rtf") as URL |
061 | |
062 | #【E: specifier】:HFS:Finder file形式 |
063 | tell application "Finder" |
064 | set fileFilePath to (file "Acknowledgements.rtf" of folder "Documentation" of folder "Library" of startup disk) as specifier |
065 | end tell |
066 | |
067 | |
068 | |
069 | #################################### |
070 | #【A: text】:path:UNIX形式のパス 実態参照しない posix path 形式 |
071 | set strFilePath to ("/Library/Documentation/Acknowledgements.rtf") as text |
072 | |
073 | #NSSTRING |
074 | set ocidFilePathStr to current application's NSString's stringWithString:(strFilePath) |
075 | set ocidFileName to ocidFilePathStr's lastPathComponent() |
076 | log ocidFileName as text |
077 | #NSPATHSTORE2 |
078 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
079 | set ocidFileName to ocidFilePath's lastPathComponent() |
080 | log ocidFileName as text |
081 | #NSURL |
082 | set ocidFilePathURL to current application's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false) |
083 | -->基本はこちらの記述 |
084 | set ocidFilePathURL to current application's NSURL's fileURLWithPath:(ocidFilePath) |
085 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
086 | log ocidFileName as text |
087 | |
088 | |
089 | |
090 | #################################### |
091 | #【B: alias】:alias:エイリアス形式のパス 実態参照するエイリアス形式 |
092 | set aliasFilePath to ("Macintosh HD:Library:Documentation:Acknowledgements.rtf") as alias |
093 | |
094 | set strFilePath to (POSIX path of aliasFilePath) as text |
095 | #NSSTRING |
096 | set ocidFilePathStr to current application's NSString's stringWithString:(strFilePath) |
097 | set ocidFileName to ocidFilePathStr's lastPathComponent() |
098 | log ocidFileName as text |
099 | #NSPATHSTORE2 |
100 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
101 | set ocidFileName to ocidFilePath's lastPathComponent() |
102 | log ocidFileName as text |
103 | #NSURL |
104 | set ocidFilePathURL to current application's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false) |
105 | -->基本はこちらの記述 |
106 | set ocidFilePathURL to current application's NSURL's fileURLWithPath:(ocidFilePath) |
107 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
108 | log ocidFileName as text |
109 | |
110 | #################################### |
111 | #【C: furl】:frul:実態参照しないエイリアス形式 |
112 | set aliasFilePath to ("Macintosh HD:Library:Documentation:Acknowledgements.rtf") as «class furl» |
113 | |
114 | set strFilePath to (POSIX path of aliasFilePath) as text |
115 | #NSSTRING |
116 | set ocidFilePathStr to current application's NSString's stringWithString:(strFilePath) |
117 | set ocidFileName to ocidFilePathStr's lastPathComponent() |
118 | log ocidFileName as text |
119 | #NSPATHSTORE2 |
120 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
121 | set ocidFileName to ocidFilePath's lastPathComponent() |
122 | log ocidFileName as text |
123 | #NSURL |
124 | set ocidFilePathURL to current application's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false) |
125 | -->基本はこちらの記述 |
126 | set ocidFilePathURL to current application's NSURL's fileURLWithPath:(ocidFilePath) |
127 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
128 | log ocidFileName as text |
129 | |
130 | #################################### |
131 | #【D: URL】:URL:URL形式 |
132 | set urlFilePath to ("file:///Library/Documentation/Acknowledgements.rtf") as URL |
133 | set strFilePathURL to (path of urlFilePath) as text |
134 | |
135 | set ocidFilePathURL to current application's NSURL's alloc()'s initWithString:(strFilePathURL) |
136 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
137 | log ocidFileName as text |
138 | -->基本はこちらの記述 |
139 | set ocidFilePathURL to current application's NSURL's URLWithString:(strFilePathURL) |
140 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
141 | log ocidFileName as text |
142 | |
143 | #################################### |
144 | #【E: specifier】:HFS:Finder file形式 |
145 | #Finder内ならaliasと同じ処理方法が可能です |
146 | tell application "Finder" |
147 | set fileFilePath to (file "Acknowledgements.rtf" of folder "Documentation" of folder "Library" of startup disk) as specifier |
148 | end tell |
149 | set aliasFilePath to fileFilePath as alias |
150 | set strFilePath to (POSIX path of aliasFilePath) as text |
151 | #NSSTRING |
152 | set ocidFilePathStr to current application's NSString's stringWithString:(strFilePath) |
153 | set ocidFileName to ocidFilePathStr's lastPathComponent() |
154 | log ocidFileName as text |
155 | #NSPATHSTORE2 |
156 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
157 | set ocidFileName to ocidFilePath's lastPathComponent() |
158 | log ocidFileName as text |
159 | #NSURL |
160 | set ocidFilePathURL to current application's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false) |
161 | -->基本はこちらの記述 |
162 | set ocidFilePathURL to current application's NSURL's fileURLWithPath:(ocidFilePath) |
163 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
164 | log ocidFileName as text |
AppleScriptで生成しました |
AppleScriptでのファイル名の取得
ソース | |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | (* |
004 | ファイルパスからのファイル名の取得 |
005 | 【A: text】:path:UNIX形式のパス 実態参照しない posix path 形式 |
006 | 【B: alias】:alias:エイリアス形式のパス 実態参照するエイリアス形式 |
007 | 【C: furl】:frul:実態参照しないエイリアス形式 |
008 | 【D: URL】:URL:URL形式 |
009 | 【E: specifier】:HFS:Finder file形式 |
010 | |
011 | com.cocolog-nifty.quicktimer.icefloe *) |
012 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
013 | ##自分環境がos12なので2.8にしているだけです |
014 | use AppleScript version "2.8" |
015 | use framework "Foundation" |
016 | use scripting additions |
017 | property refMe : a reference to current application |
018 | |
019 | |
020 | #【A: text】:path:UNIX形式のパス 実態参照しない posix path 形式 |
021 | set strFilePath to ("/Library/Documentation/Acknowledgements.rtf") as text |
022 | |
023 | #【B: alias】:alias:エイリアス形式のパス 実態参照するエイリアス形式 |
024 | set aliasFilePath to ("Macintosh HD:Library:Documentation:Acknowledgements.rtf") as alias |
025 | |
026 | #【C: furl】:frul:実態参照しないエイリアス形式 |
027 | set aliasFilePath to ("Macintosh HD:Library:Documentation:Acknowledgements.rtf") as «class furl» |
028 | |
029 | #【D: URL】:URL:URL形式 |
030 | #このClassはscripting additionsが必要になります |
031 | set urlFilePath to ("file:///Library/Documentation/Acknowledgements.rtf") as URL |
032 | |
033 | #【E: specifier】:HFS:Finder file形式 |
034 | tell application "Finder" |
035 | set fileFilePath to (file "Acknowledgements.rtf" of folder "Documentation" of folder "Library" of startup disk) as specifier |
036 | end tell |
037 | |
038 | #【F:NSString】OBJC NSString=テキスト |
039 | #【G:NSPathStore2】OBJC NSPathStore2=パス(テキスト) |
040 | #【H:NSURL】OBJC NSURL |
041 | |
042 | #################################### |
043 | #【A: text】:path:UNIX形式のパス 実態参照しない posix path 形式 |
044 | #注意:pathの場合実態参照しないのでパスの先が存在しない可能性がある |
045 | #そのため、処理に『よっては』エラーになるのを考慮する必要がある |
046 | |
047 | set strFilePath to ("/Library/Documentation/Acknowledgements.rtf") as text |
048 | log class of strFilePath |
049 | --> text |
050 | log strFilePath |
051 | |
052 | #delimiters区切り文字でリスト化して最後の項目 |
053 | set strDelim to AppleScript's text item delimiters |
054 | set AppleScript's text item delimiters to "/" |
055 | set listFilePathComponent to every text item of strFilePath |
056 | set AppleScript's text item delimiters to strDelim |
057 | set strFileName to (last item of listFilePathComponent) as text |
058 | log strFileName |
059 | --> "Acknowledgements.rtf" |
060 | |
061 | |
062 | #sh ファイル名の取得 |
063 | #パスの一部分として取り出す |
064 | set strFileName to (do shell script "/usr/bin/basename \"" & strFilePath & "\"") as text |
065 | log strFileName |
066 | --> "Acknowledgements.rtf" |
067 | |
068 | #sh パスではなくテキストとしてみた場合 |
069 | #パラメータ展開を使ってスクリプト形式で処理する(邪道) |
070 | set strFileName to (do shell script "PATH_INPUT=\"" & strFilePath & "\";STR_FILENAME=\"${PATH_INPUT##*/}\";echo \"$STR_FILENAME\"") as text |
071 | log strFileName |
072 | |
073 | |
074 | |
075 | #################################### |
076 | #【B: alias】:alias:エイリアス形式のパス 実態参照するエイリアス形式 |
077 | set aliasFilePath to ("Macintosh HD:Library:Documentation:Acknowledgements.rtf") as alias |
078 | log class of aliasFilePath |
079 | --> alias |
080 | log aliasFilePath |
081 | |
082 | #scripting additionsのinfo forを使う |
083 | set recordInfo to (info for aliasFilePath) as record |
084 | #ファイル名の取得 |
085 | set strFileName to (name of recordInfo) as text |
086 | log strFileName |
087 | --> "Acknowledgements.rtf" |
088 | |
089 | #Finderでnameを取得 |
090 | tell application "Finder" |
091 | set strFileName to (name of aliasFilePath) as text |
092 | end tell |
093 | log strFileName |
094 | --> "Acknowledgements.rtf" |
095 | |
096 | #System Events nameの取得 |
097 | tell application "System Events" |
098 | set strFileName to (name of aliasFilePath) as text |
099 | end tell |
100 | log strFileName |
101 | --> "Acknowledgements.rtf" |
102 | |
103 | |
104 | |
105 | #################################### |
106 | #【C: furl】:frul:実態参照しないエイリアス形式 |
107 | #注意:furlの場合実態参照しないのでパスの先が存在しない可能性がある |
108 | #そのため、処理に『よっては』エラーになるのを考慮する必要がある |
109 | #実態参照しないため、Finder等では、そのままではname=ファイル名が取得できない |
110 | set aliasFilePath to ("Macintosh HD:Library:Documentation:Acknowledgements.rtf") as «class furl» |
111 | log class of aliasFilePath |
112 | --> «class furl» |
113 | log aliasFilePath |
114 | |
115 | #scripting additionsのinfo forを使う |
116 | set recordInfo to (info for aliasFilePath) as record |
117 | set strFileName to (name of recordInfo) as text |
118 | log strFileName |
119 | --> "Acknowledgements.rtf" |
120 | |
121 | #Finderでnameを取得 |
122 | tell application "Finder" |
123 | try |
124 | set strFileName to (name of aliasFilePath) as text |
125 | -->エラーになる |
126 | on error strErrMes number numErrNo |
127 | #エイリアス=実態参照させてからファイル名を取得する |
128 | set strFileName to (name of (aliasFilePath as alias)) as text |
129 | end try |
130 | end tell |
131 | log strFileName |
132 | --> "Acknowledgements.rtf" |
133 | |
134 | #System Events nameの取得 |
135 | tell application "System Events" |
136 | try |
137 | set strFileName to (name of aliasFilePath) as text |
138 | -->エラーになる |
139 | on error strErrMes number numErrNo |
140 | #エイリアス=実態参照させてからファイル名を取得する |
141 | set strFileName to (name of (aliasFilePath as alias)) as text |
142 | end try |
143 | end tell |
144 | |
145 | log strFileName |
146 | --> "Acknowledgements.rtf" |
147 | |
148 | |
149 | #################################### |
150 | #【D: URL】:URL:URL形式 |
151 | #scripting additionsの機能を使いますので |
152 | #use scripting additions の記述は必要になります |
153 | # |
154 | set urlFilePath to ("file:///Library/Documentation/Acknowledgements.rtf") as URL |
155 | log class of urlFilePath |
156 | --> URL |
157 | log urlFilePath |
158 | -->{class:URL, scheme:file URL (obsolete), path:file:///Library/Documentation/Acknowledgements.rtf} |
159 | #戻り値がレーコード(properties)形式でnameが含まれていない場合は |
160 | #パスにしてから |
161 | set strFilePathURL to (path of urlFilePath) as text |
162 | |
163 | #テキスト形式のパスと同様の処理方法 |
164 | #delimiters区切り文字でリスト化して最後の項目 |
165 | set strDelim to AppleScript's text item delimiters |
166 | set AppleScript's text item delimiters to "/" |
167 | set listFilePathComponent to every text item of strFilePathURL |
168 | set AppleScript's text item delimiters to strDelim |
169 | set strFileName to (last item of listFilePathComponent) as text |
170 | log strFileName |
171 | --> "Acknowledgements.rtf" |
172 | |
173 | # zshの ${変数:修飾子}で取得する |
174 | set strFileName to (do shell script "/bin/zsh -c 'STR_URL=\"" & strFilePathURL & "\";echo ${STR_URL:t}'") as text |
175 | log strFileName |
176 | |
177 | #UNIX形式のファイルパスにしてから処理する方法もあります |
178 | set strDelim to AppleScript's text item delimiters |
179 | set AppleScript's text item delimiters to "/" |
180 | set listFilePathComponent to every text item of strFilePathURL |
181 | set listFilePath to (items 3 through -1 of listFilePathComponent) as list |
182 | set strFilePath to listFilePath as text |
183 | set AppleScript's text item delimiters to strDelim |
184 | log strFilePath |
185 | -->/Library/Documentation/Acknowledgements.rtf |
186 | #エイリアスにしても良いでしょう |
187 | set recordInfo to (info for (POSIX file strFilePath as alias)) as record |
188 | set strFileName to (name of recordInfo) as text |
189 | log strFileName |
190 | --> "Acknowledgements.rtf" |
191 | |
192 | |
193 | #################################### |
194 | #【E: specifier】:HFS:Finder file形式 |
195 | #Finder内ならaliasと同じ処理方法が可能です |
196 | tell application "Finder" |
197 | set fileFilePath to (file "Acknowledgements.rtf" of folder "Documentation" of folder "Library" of startup disk) as specifier |
198 | set strFileName to (name of fileFilePath) as text |
199 | end tell |
200 | log strFileName |
201 | --> "Acknowledgements.rtf" |
AppleScriptで生成しました |
| 固定リンク