Applescript:拡張機能なしでFilenamesをフォルダーに入手します
-
28-09-2019 - |
質問
これを行うことで、フォルダー内のすべてのファイルの名前を取得できます。
tell application "Finder"
set myFiles to name of every file of somePath
end tell
文字列を変更するにはどうすればよいですか myFiles
ファイル拡張機能を含めないようにしますか?
たとえば、取得できます {"foo.mov", "bar.mov"}
, 、しかし持ちたいです {"foo", "bar"}
.
現在の解決策
受け入れられた答えに基づいて、以下のコードを思いつきました。どういうわけか、それがよりきれいになるか、より効率的にできるかどうかを教えてください。
-- Gets a list of filenames from the
on filenames from _folder
-- Get filenames and extensions
tell application "Finder"
set _filenames to name of every file of _folder
set _extensions to name extension of every file of _folder
end tell
-- Collect names (filename - dot and extension)
set _names to {}
repeat with n from 1 to count of _filenames
set _filename to item n of _filenames
set _extension to item n of _extensions
if _extension is not "" then
set _length to (count of _filename) - (count of _extension) - 1
set end of _names to text 1 thru _length of _filename
else
set end of _names to _filename
end if
end repeat
-- Done
return _names
end filenames
-- Example usage
return filenames from (path to desktop)
解決
これがあなたが望んだことをする完全なスクリプトです。誰かが解決策として提供する簡単なワンライナーがあると思ったので、私は元々それを投稿することに消極的でした。うまくいけば、この解決策はそうではありません ルーベ・ゴールドバーグ 物事のやり方。
Finder Dictionaryにはを持っています 名前拡張機能 あなたが次のようなことをすることができるようにプロパティ
tell application "Finder"
set myFiles to name extension of file 1 of (path to desktop)
end tell
したがって、上記は、ユーザーのデスクトップ上の最初のファイルの拡張機能を取得します。 (ベース名 - 拡張子)を取得するための単純な関数があるように思われますが、それは見つかりませんでした。
ディレクトリ全体のすべてのファイルに対して拡張機能なしでファイル名を取得するためのスクリプトは次のとおりです。
set filesFound to {}
set filesFound2 to {}
set nextItem to 1
tell application "Finder"
set myFiles to name of every file of (path to desktop) --change path to whatever path you want
end tell
--loop used for populating list filesFound with all filenames found (name + extension)
repeat with i in myFiles
set end of filesFound to (item nextItem of myFiles)
set nextItem to (nextItem + 1)
end repeat
set nextItem to 1 --reset counter to 1
--loop used for pulling each filename from list filesFound and then strip the extension
--from filename and populate a new list called filesFound2
repeat with i in filesFound
set myFile2 to item nextItem of filesFound
set myFile3 to text 1 thru ((offset of "." in myFile2) - 1) of myFile2
set end of filesFound2 to myFile3
set nextItem to (nextItem + 1)
end repeat
return filesFound2
上記のスクリプトは、OPが望んでいたことをするより簡単な方法を知っていれば、上記のスクリプトは機能しますが、それを投稿してください。たぶん、これを容易にするスクリプトの追加があるかもしれません。誰かが知っていますか?
他のヒント
から http://www.macosxautomation.com/applescript/sbrt/index.html :
on remove_extension(this_name)
if this_name contains "." then
set this_name to ¬
(the reverse of every character of this_name) as string
set x to the offset of "." in this_name
set this_name to (text (x + 1) thru -1 of this_name)
set this_name to (the reverse of every character of this_name) as string
end if
return this_name
end remove_extension
これは、剥がれたファイル名が何であるかについてファインダーのアイデアを得るためのりんごの描写方法です。
set extension hidden of thisFile to true
set thisName to displayed name of thisFile
-- display dialog "hey"
set extension hidden of thisFile to false
シングルラインの方法、ファインダーなし、システムイベントなし。より効率的で高速です。副作用(良い、または悪い可能性がある可能性があります):「。このキャラクターが剥奪されます。 「すべての文字の逆」を使用すると、名前が複数の期間として機能する場合に機能します。
set aName to text 1 thru ((aName's length) - (offset of "." in ¬
(the reverse of every character of aName) as text)) of aName
名前のリストを処理するハンドラーとしてのソリューション:
on RemoveNameExt(aList)
set CleanedList to {}
repeat with aName in aList
set the end of CleanedList to text 1 thru ((aName's length) - (offset of ¬
"." in (the reverse of every character of aName) as text)) of aName
end repeat
return CleanedList
end RemoveNameExt
「すべてのファイル」構文を使用するときに拡張機能を削除する方法がわかりませんが、各ファイルを介してループ(ループが表示されない)を気にしない場合、これは機能します。
tell application "Finder"
set myFile to name of file 1 of somePath
set myFile2 to text 1 thru ((offset of "." in myFile) - 1) of myFile
end tell
ラウリ・ランタのものに基づいています 素敵な解決策, 、Finderが知らない拡張機能で機能します。
set delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set myNames to {}
tell application "Finder"
set myFiles to name of every file of (path to Desktop)
repeat with myfile in myFiles
set myname to name of file myfile
if myname contains "." then set myname to (text items 1 thru -2 of myname) as text
set end of myNames to myname
end repeat
end tell
set AppleScript's text item delimiters to delims
return myNames
Tell "Finder"ブロック内で、これはmyNamesで拡張機能を削除したファイル名を収集します。
repeat with f in myFiles
set myNames's end to ¬
(f's name as text)'s text 1 thru -(((f's name extension as text)'s length) + 2)
end repeat