In a folder, with subfolders, I am trying to find filenames with spaces in the beginning or at the end of the filename.
Then I am trying to put these filenames into a list, with their originating subfolders, so that I can find those filenames.
The following is the code I have so far, but I am getting stuck.
set someText to ""
set f to (choose folder)
tell application "Finder" to set e to f's entire contents
set filelist to {"Contents of " & f & return & return}
on trim(someText)
local filelist
repeat until someText starts with " "
set someText to text 2 thru -1 of someText
end repeat
repeat until someText ends with " "
set someText to text 1 thru -2 of someText
end repeat
end trim
repeat with i from 1 to (count e)
set end of filelist to ("" & someText & return)
end repeat
set the clipboard to "" & filelist
beep 2
tell application "TextEdit"
make new document at beginning with properties {text:filelist as Unicode text}
activate
end tell
set f to (choose folder)
tell application "Finder" to set e to f's entire contents
set filelist to {"Contents of " & f & return & return}
repeat with i from 1 to (count of e)
tell application "Finder" to set n to name of ((e's item i) as alias)
if (n starts with " ") or (n ends with " ") then set end of filelist to ("" & my trim(n) & return)
end repeat
set the clipboard to "" & filelist
beep 2
tell application "TextEdit"
activate
make new document at beginning with properties {text:filelist as Unicode text}
end tell
on trim(someText)
repeat while someText starts with " "
set someText to text 2 thru -1 of someText
end repeat
repeat while someText ends with " "
set someText to text 1 thru -2 of someText
end repeat
return someText
end trim
Yvan KOENIG (from FRANCE mardi 31 octobre 2006 18:41:06)
set f to (choose folder)
tell application "Finder" to set e to files of f's entire contents whose (name begins with " " or name ends with " ")
if (count e) > 0 then
set filelist to {"Contents of " & f & return & return}
repeat with i in e
set end of filelist to ((i as string) & return)
end repeat
set the clipboard to filelist
beep 2
tell application "TextEdit"
activate
make new document at beginning with properties {text:filelist as Unicode text}
end tell
else
display dialog "no matches found"
end if
I’m not at a Mac right now, but I think this is better (no repeat loop):
set sourceFolder to choose folder
tell application "Finder" to set fileList to files of sourceFolder's entire contents whose (name begins with " " or name ends with " ")
if fileList is {} then display dialog "No matches found." buttons {"Cancel"} default button 1
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set fileList to "Contents of " & sourceFolder & return & return & fileList
set AppleScript's text item delimiters to ASTID
set the clipboard to fileList
beep 2
tell application "TextEdit"
activate
make new document at beginning with properties {text:fileList}
end tell
Although simple to implement, a double whose filter in the Finder can take quite a while if there are a lot of files in the hierarchy. A much faster approach “ although regrettably with more code “ is to do a mass-coercion of all the hierarchy’s files to Unicode text and then use vanilla methods to parse and edit the result.
set sourceFolder to choose folder
set Ureturn to return as Unicode text
set Ucolon to ":" as Unicode text
set U2spaces to " " as Unicode text
script o
property fileList : missing value
end script
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to Ureturn
tell application "Finder" to set o's fileList to paragraphs of (files of sourceFolder's entire contents as Unicode text)
set AppleScript's text item delimiters to Ucolon
considering case
repeat with i from 1 to (count o's fileList)
set thisPath to item i of o's fileList
if (thisPath ends with Ucolon) then -- Just in case the file's a bundle.
set thisName to text item -2 of thisPath
else
set thisName to text item -1 of thisPath
end if
if (thisName begins with U2spaces or thisName ends with U2spaces) then
else
set item i of o's fileList to missing value
end if
end repeat
end considering
set o's fileList to o's fileList's every Unicode text
if (o's fileList is {}) then display dialog "No matches found." buttons {"Cancel"} default button 1
set AppleScript's text item delimiters to return
set listText to ("Contents of " as Unicode text) & sourceFolder & return & return & o's fileList
set AppleScript's text item delimiters to ASTID
tell application "TextEdit"
activate
make new document at beginning with properties {text:listText}
end tell