Saturday, July 4, 2020

#1 2020-04-24 04:42:13 pm

jprokos
Member
Registered: 2019-09-02
Posts: 2

"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. 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.

Applescript:

#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

Last edited by jprokos (2020-04-24 05:08:02 pm)

Offline

 

#2 2020-04-24 07:29:05 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 530

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

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:

Applescript:

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.


2018 Mac mini - macOS Catalina

Online

 

#3 2020-04-24 08:18:16 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6395

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

peavine wrote:

Finder does not return hidden files



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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#4 2020-04-24 11:55:08 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

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

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

3) The default AppleScript text item delimiters is not {} but "" or {""}
4) 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:

Applescript:


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

Applescript:


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:

Applescript:


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.

Last edited by KniazidisR (2020-04-25 04:07:01 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)