"Set x to name of every file" finding non existent file

Hi everyone. I haven’t posted here in a very long time, but need some help. :slight_smile:

I thought using “tell application “system events”” instead of "tell application “finder” was causing the returned list to show the invisible “.DS_Store” file in results but that wasn’t the case. It appears to be the forward slashes in one of the file names that needs to be handled. Not sure how to accomplish this in the script without renaming the actual file.

The culprit is this file name: “file-///Volumes/Godzilla/iTu.textClipping”

This file was picked up but also a ghost file/result. Result {“”, “textClipping”}. The file was then excluded from the following actions (which I have omitted here for brevity) and wreaked havoc on the results.

#reset delimeters and variables
set AppleScript's text item delimiters to {} --reset delimeter
set excluded_ to {".DS_Store"}
set source to {}
set fnames to {}

display dialog "Use a file's name to create a folder and put the file inside. " & return & "Next: Choose the directory containing the files." buttons {"Cancel", "Next"} default button "Next"

#ask user to choose directory with files and store in variable "source"
set source to (choose folder) as alias

#get file names and store them in variable "fnames"

tell application "System Events"
	set x to (name extension of every file of source)
end tell
x

tell application "Finder"
	set fnames to (name of every file of source whose name is not in excluded_)
end tell

Model: iMac
AppleScript: 2.11 (203.1)
Browser: Safari 605.1.15
Operating System: macOS 10.14

jprokos. I was not able to fully follow your request but I thought I’d make some comments FWIW.

System Events does return hidden files but this can be avoided as follows:

set theFolder to choose folder -- returns an alias

tell application "System Events"
	set fileNames to name of every file in theFolder whose visible is true
	set fileExtensions to name extension of every file in theFolder whose visible is true
end tell

Finder does not return hidden files and so it would not appear necessary to include .DS_Store" in the Finder exclude list. You can include other files if that’s what you want to do.

It’s not pertinent to your issue, but choose folder returns an alias, making coercion to an alias unnecessary.

That’s not quite right: it does return them if you have them showing in the Finder. (This is a good reason not to use the Finder generally, because the results depend on a UI setting.)

Hi, jprokos.

  1. Is your file hidden too? Begins its name with dot like .DS_Store files? If it is hidden, then the script above should be fixed.

  2. Your file name (but it is URL, and not name) can’t be as showed above. It should be like this:

“file:///Volumes/Godzilla/iTu.textClipping” – Here Godzilla is disk name or server name

if you file is hidden, then it should be like this:

“file:///Volumes/Godzilla/.iTu.textClipping” – note the dot before iTu

  1. The default AppleScript text item delimiters is not {} but “” or {“”}
  2. You can provide prompt to choose folder command.

Here is the script, which lists all file names including hidden and visible files of chosen folder except .DS_Store files:


set AppleScript's text item delimiters to {""} --reset delimeter
set excluded_ to {".DS_Store"}
set fnames to {} -- filtered names

display dialog "Use a file's name to create a folder and put the file inside." buttons {"Cancel", "Next"} default button "Next"
set sourceFolder to choose folder with prompt "Choose the directory containing the files." -- returns an alias

tell application "System Events"
	set fileNames to name of every file in sourceFolder
	set fileExtensions to name extension of every file in sourceFolder
end tell

repeat with aName in fileNames
	if not (aName is in excluded_) then set end of fnames to contents of aName
end repeat

return fnames

NOTE: If you need to filter only .DS_Store files, then everything can be done easier and faster (without using repeat loop):


set AppleScript's text item delimiters to {""} --reset delimeter

display dialog "Use a file's name to create a folder and put the file inside." buttons {"Cancel", "Next"} default button "Next"
set sourceFolder to choose folder with prompt "Choose the directory containing the files."

tell application "System Events"
	set fileExtensions to name extension of every file in sourceFolder
	set fnames to name of every file of sourceFolder whose name is not ".DS_Store"
end tell

And finally, here’s how to filter out multiple names (here, 3) using clause whose:


set AppleScript's text item delimiters to {""} --reset delimeter

display dialog "Use a file's name to create a folder and put the file inside." buttons {"Cancel", "Next"} default button "Next"
set sourceFolder to choose folder with prompt "Choose the directory containing the files."

tell application "System Events"
	set fileExtensions to name extension of every file in sourceFolder
	set fnames to name of every file of sourceFolder whose (name is not ".DS_Store") and (name is not ".mailcap") and (name is not "untitled.mp3")
end tell

I should note finally that your original code has 1 main problem - the whose clause can’t work with lists and conditions like is in, or is contained and so on. That is why it doesn’t work. So, if you have to filter few names, then the best choice is my last script. If you have mutable list of names to filter then you should use my 1st script.