Tuesday, December 10, 2019

#1 2019-11-29 04:41:38 pm

sweyhrich
Member
From:: Omaha, NE
Registered: 2003-11-21
Posts: 39
Website

Filename fun

I'm working on a script that is to do date processing on every file in a folder. I've got this skeleton:

[Applescript]
set inputFolder to "Thorin:users:a2history:Desktop:Export:"
tell application "Finder" to set foundImages to files of entire contents of folder inputFolder
repeat with i in foundImages
   
    set theImage to i as alias
   
    -- process file
   
end repeat
[/AppleScript]

When I try to run this, the first file it encounters has this title:

18841108120000 James Rasmus Paulsen US citizenship papers.jpeg

and it gives me this error message:

Can’t make quoted form of alias "Thorin:Users:a2history:Desktop:Export:18841108120000 James Rasmus Paulsen US citizenship papers.jpeg" into type Unicode text.



I suspect it is having problems with all of the spaces in the name, but I am not sure how to make the Applescript happy.

Suggestions are appreciated.

Last edited by sweyhrich (2019-11-29 04:44:57 pm)


Steven Weyhrich <IX0YE><
MacBook, Mac OS X 10.15.1
Apple II History
http://apple2history.org

Offline

 

#2 2019-11-29 06:09:21 pm

maro
Member
From:: Nerima, Tokyo, Japan
Registered: 2004-05-30
Posts: 41
Website

Re: Filename fun

I think you quoted not a POSIX path but an alias.

The error indicates "quoted form of alias" (error).

Your code make alias from file (gathered by entire contents).
Then you seem to make POSIX path with "quoted form of" but a "POSIX path of", I think.


Applescript:


set a to "Macintosh HD:Users:maro:Desktop:"
set bList to {}
tell application "Finder"
   set aList to files of entire contents of folder a
   --> {document file "18841108120000 James Rasmus Paulsen US citizenship papers.png" of folder "Desktop" of folder "me" of folder "Users" of startup disk of application "Finder", ....}
   
   repeat with i in aList
       set aFile to (i as alias)
       
       set aPOSIX to (quoted form of POSIX path of aFile) --Important!!
       set the end of bList to aPOSIX
   end repeat
   
end tell

return bList

Model: Mac mini 2014 on macOS 10.15.1
AppleScript: 2.7
Browser: Safari 13.0.3
Operating System: Other


I wrote thousands of AppleScript to realize my idea. Natural language interface, voice recognition commander and so on. Though my mother toungue is strange language, Japanese, my most frequently write language is AppleScript. I believe it is for making things easy and powerful.


Filed under: Posix path

Offline

 

#3 2019-11-29 06:20:42 pm

maro
Member
From:: Nerima, Tokyo, Japan
Registered: 2004-05-30
Posts: 41
Website

Re: Filename fun

Your path conversion: file(from entire contents) --> alias --> quoted form of !!!!

Correct path conversion: file(from entire contents) --> alias --> POSIX path --> quoted form of

filePathCastingFig-768x434.png

Model: MacBook Pro 2012
AppleScript: 2.7
Browser: Safari 13.0.1
Operating System: macOS 10.14


I wrote thousands of AppleScript to realize my idea. Natural language interface, voice recognition commander and so on. Though my mother toungue is strange language, Japanese, my most frequently write language is AppleScript. I believe it is for making things easy and powerful.


Filed under: Posix path, file path

Offline

 

#4 2019-11-30 09:48:25 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11627
Website

Re: Filename fun

Actually your code must not throw any errors. The reason of the error is somewhere else.

Spaces in HFS paths don't matter. The are crucial only in do shell script lines.

By the way if the base folder is a folder on the current desktop you can simply write

Applescript:

tell application "Finder" to set foundImages to files of entire contents of folder "Export"

because the desktop folder of the current user is the root folder of the Finder


regards

Stefan

Offline

 

#5 2019-11-30 11:27:37 am

sweyhrich
Member
From:: Omaha, NE
Registered: 2003-11-21
Posts: 39
Website

Re: Filename fun

Thank you for the comments!

Maro - questions about your code. Tell me if I understand this correctly:

1) So this is setting the variable "alist" to contain the names of all of the files in folder "a"

   set aList to files of entire contents of folder a


2) This is looping through all of the files in the list "alist" and doing something with each one, with the pointer "I", going from 1 to the end of the list.

   repeat with i in aList
   ...
   end repeat

3) When running this, I see that both "I" and "aFile" seem to contain the same information, the full pathname for the file, except aFile seems to have a <return> at the start of it. The different folders are separated by ":"

  repeat with i in aList
       set aFile to (i as alias)


4) This gets the filename more in the typical Mac format, with no root directory included, and all of the folders separated by "/"

     set aPOSIX to (quoted form of POSIX path of aFile) --Important!!
 
5) I don't understand the "end of bList" part of this:

      set the end of bList to aPOSIX

When I run this and do a Display Dialog blist after each loop, bList contains each additional filename appended to the end of the last one. This doesn't seem right.

For example, let's take a simple file structure and simple filenames.

/Users/Folder/

where Folder contains

a.jpg
b.jpg
c.jpg
d.jpg

On each loop through, bList looks like this:

1st time - '/Users/Folder/a.jpg'
2nd time - "/Users/Folder/a.jpg''/Users/Folder/b.jpg'
3rd time - "/Users/Folder/a.jpg''/Users/Folder/b.jpg''/Users/Folder/c.jpg'
4th time - "/Users/Folder/a.jpg''/Users/Folder/b.jpg''/Users/Folder/c.jpg''/Users/Folder/d.jpg'

It appears that aPOSIX has the filename that I need, and I am unclear as to what is the need of the bList variable, since it just makes the file list longer and longer and longer. Can you clarify?

Also, what am I doing wrong? I used the [Applescript][/Applescript] codes, and my code did not display correctly, but your code does. What is the correct way to include code here on Macscripter?

Thank you


Steven Weyhrich <IX0YE><
MacBook, Mac OS X 10.15.1
Apple II History
http://apple2history.org

Offline

 

#6 2019-11-30 11:53:27 am

sweyhrich
Member
From:: Omaha, NE
Registered: 2003-11-21
Posts: 39
Website

Re: Filename fun

Also, how do I isolate just the filename from that aPOSIX variable? That is, if aPOSIX = "/Users/Folder/a.jpg", I need just "a.jpg" in a different variable.


Steven Weyhrich <IX0YE><
MacBook, Mac OS X 10.15.1
Apple II History
http://apple2history.org

Offline

 

#7 2019-11-30 11:55:53 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3673

Re: Filename fun

I am a bit puzzled by what you report but I think that I found the explanation.

Applescript:

set inputFolder to (path to desktop folder as text) & "aaaaa"
tell application "Finder" to set foundImages to files of entire contents of folder inputFolder as alias list
--> {alias "SSD 1000:Users:**********:Desktop:aaaaa:CombinePDFs.applescript", alias "SSD 1000:Users:**********:Desktop:aaaaa:CombinePDFs.scpt", alias "SSD 1000:Users:**********:Desktop:aaaaa:Combined.pdf", alias "SSD 1000:Users:**********:Desktop:aaaaa:split PDF.applescript", alias "SSD 1000:Users:**********:Desktop:aaaaa:split PDF.scpt"}
set bList to {}
repeat with aFile in foundImages
   set aPosix to quoted form of POSIX path of aFile
   set end of bList to aPosix
end repeat
bList

--> {"'/Users/**********/Desktop/aaaaa/CombinePDFs.applescript'", "'/Users/**********/Desktop/aaaaa/CombinePDFs.scpt'", "'/Users/**********/Desktop/aaaaa/Combined.pdf'", "'/Users/**********/Desktop/aaaaa/split PDF.applescript'", "'/Users/**********/Desktop/aaaaa/split PDF.scpt'"}
log bList as text
--> (*'/Users/**********/Desktop/aaaaa/CombinePDFs.applescript''/Users/**********/Desktop/aaaaa/CombinePDFs.scpt''/Users/**********/Desktop/aaaaa/Combined.pdf''/Users/**********/Desktop/aaaaa/split PDF.applescript''/Users/**********/Desktop/aaaaa/split PDF.scpt'*)

My understanding is that you are fooled by the way the log instruction behaves, which concatenate the different paths stored in the list using an empty string as delimiter.
This delimiter is not visible so it gives what you see.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 30 novembre 2019  18:45:16

Offline

 

#8 2019-11-30 12:05:05 pm

sweyhrich
Member
From:: Omaha, NE
Registered: 2003-11-21
Posts: 39
Website

Re: Filename fun

Okay, let me clarify.

Here is the code as I've got it in Script Editor:

[applescript]set a to "Thorin:Users:a2history:Desktop:Import:"
set bList to {}
tell application "Finder"
    set aList to files of entire contents of folder a
    --> {document file "18841108120000 James Rasmus Paulsen US citizenship papers.png" of folder "Desktop" of folder "me" of folder "Users" of startup disk of application "Finder", ....}
   
    repeat with i in aList
        set aFile to (i as alias)
       
        set aPOSIX to (quoted form of POSIX path of aFile) --Important!!
       
        set the end of bList to aPOSIX
       
        display dialog "aPOSIX = " & aPOSIX
       
        display dialog "bList = " & bList
       
    end repeat
   
end tell

return bList
[/AppleScript]

On first loop, Display Dialog shows me these two lines:

aPOSIX = '/Users/a2history/Desktop/Import/a.jpg'
bList = '/Users/a2history/Desktop/Import/a.jpg'

On second loop:

aPOSIX = '/Users/a2history/Desktop/Import/b.jpg'
bList = '/Users/a2history/Desktop/Import/a.jpg''/Users/a2history/Desktop/Import/b.jpg'

On third loop:

aPOSIX = '/Users/a2history/Desktop/Import/c.jpg'
bList='/Users/a2history/Desktop/Import/a.jpg''/Users/a2history/Desktop/Import/b.jpg''/Users/a2history/Desktop/Import/c.jpg'

On fourth loop:

aPOSIX = '/Users/a2history/Desktop/Import/d.jpg'
bList='/Users/a2history/Desktop/Import/a.jpg''/Users/a2history/Desktop/Import/b.jpg''/Users/a2history/Desktop/Import/c.jpg''/Users/a2history/Desktop/Import/d.jpg'


So, each increment through the loop adds the new file path to the end of the previous one in bList, which makes it unclear to me as to why bList is useful. Is it supposed to get me just the name of the file, i.e., "a.jpg", or "b.jpg"?


Steven Weyhrich <IX0YE><
MacBook, Mac OS X 10.15.1
Apple II History
http://apple2history.org

Offline

 

#9 2019-11-30 12:58:23 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3673

Re: Filename fun

You are fooled by the instruction :
  display dialog "bList = " & bList

which, as I already wrote concatenate the items of the list using an empty string as separator.

Try with :

Applescript:

set a to "Thorin:Users:a2history:Desktop:Import:"
tell application "Finder" to set foundImages to files of entire contents of folder a as alias list

set bList to {}
repeat with aFile in foundImages
   set aPosix to quoted form of POSIX path of aFile
   set end of bList to aPosix
   display dialog "bList = " & bList # Your instruction
   display dialog "bList = " & my recolle(bList, linefeed) # Enhanced one
end repeat
bList

#=====

on recolle(l, d)
   local oTIDs, t
   set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
   set t to l as text
   set AppleScript's text item delimiters to oTIDs
   return t
end recolle

#=====

You will see what your original syntax and what my enhanced one do.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 30 novembre 2019  19:58:00

Offline

 

#10 2019-11-30 01:34:11 pm

sweyhrich
Member
From:: Omaha, NE
Registered: 2003-11-21
Posts: 39
Website

Re: Filename fun

Yvan, thanks for the reply.

I did

Display dialog "bList = " & blist

because I like to be clear what display dialog is showing. But if I just make it

display dialog bList

I am not using any "&", and it STILL gradually adds each path to the previous one:

'/Users/Folder/a.jpg'
"/Users/Folder/a.jpg''/Users/Folder/b.jpg'
"/Users/Folder/a.jpg''/Users/Folder/b.jpg''/Users/Folder/c.jpg'
"/Users/Folder/a.jpg''/Users/Folder/b.jpg''/Users/Folder/c.jpg''/Users/Folder/d.jpg'

I see how your routine displays it properly, but I don't really need bList to get larger with each swing through the loop. All I really need from the code that goes through the folder is to have one variable with the full pathname, and a second variable with the filename only. That is, on one run though the folder:

aPOSIX should contain "/Users/Folder/a.jpg"

and

bFile should contain "a.jpg"

I don't really need a gradually enlarging, concatenated list of all of the files in the folder.


Steven Weyhrich <IX0YE><
MacBook, Mac OS X 10.15.1
Apple II History
http://apple2history.org

Offline

 

#11 2019-11-30 02:01:47 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3673

Re: Filename fun

I am not a sooth sayer so I was unable to guess that you just wanted to get the fileName and the quoted POSIX path.

Applescript:

set a to "Thorin:Users:a2history:Desktop:Import:" -- (path to desktop as text) & "aaaaa"
tell application "Finder" to set foundImages to files of entire contents of folder a as alias list

repeat with aFile in foundImages
   -- set fName to name of (info for aFile)
   -- tell application "Finder" to set fName to name of aFile
   tell application "System Events" to set fName to name of aFile # Always consistent with the POSIX path !
   set aPosix to quoted form of POSIX path of aFile
   
   # Do what you want with the fileName and the quoted POSIX path
   display dialog "posixPath : " & aPosix & linefeed & "fileName : " & fName
end repeat

I gave three ways to grab the file name.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 30 novembre 2019  21:01:41

Last edited by Yvan Koenig (2019-11-30 02:02:18 pm)

Offline

 

#12 2019-11-30 02:56:42 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11627
Website

Re: Filename fun

sweyhrich wrote:


Also, what am I doing wrong? I used the [Applescript][/Applescript] codes, and my code did not display correctly, but your code does. What is the correct way to include code here on Macscripter?



All tags are lowercase (applescript).

You can also highlight the code and press the Applescript button


regards

Stefan

Offline

 

#13 2019-11-30 03:51:48 pm

sweyhrich
Member
From:: Omaha, NE
Registered: 2003-11-21
Posts: 39
Website

Re: Filename fun

Yvan, thank you for the information and correct form for doing that.

Stefan, I had clicked the "Applescript" button, and when I did that, it put the tags , down, and when I pressed "space", it appears that the autocorrect on my Mac capitalized the second set of tags; when I saw that, I thought they were both supposed to be captitalized and so "fixed" it.


Steven Weyhrich <IX0YE><
MacBook, Mac OS X 10.15.1
Apple II History
http://apple2history.org

Offline

 

#14 2019-11-30 07:39:34 pm

sweyhrich
Member
From:: Omaha, NE
Registered: 2003-11-21
Posts: 39
Website

Re: Filename fun

Hi, back again.

I discovered an Applescript progress bar, which also included a (perhaps better) method of selecting files for this purpose.

However, I am getting an error when I try to connect a file to the variables. Yvan or others, can you see what is the problem with this code fragment:

Applescript:

set theImages to choose file with prompt "Please select some images to process:" of type {"public.image"} with multiple selections allowed

-- Update the initial progress information
set theImageCount to length of theImages
set progress total steps to theImageCount
set progress completed steps to 0
set progress description to "Processing Images..."
set progress additional description to "Preparing to process."

repeat with aFile from 1 to length of theImages
   
   -- Update the progress detail
   set progress additional description to "Processing image " & aFile & " of " & theImageCount
   
   -- Process the image
   
   -- tell application "Finder" to set aTitle to name of aFile
   -- tell application "System Events" to set aTitle to name of aFile # Always consistent with the POSIX path !
   set aTitle to name of (info for aFile)
   set aPosix to quoted form of POSIX path of aFile
   
   display dialog "posixPath : " & aPosix & linefeed & "fileName : " & aTitle
   
   -- get date and time from filename
   
   -- Increment the progress
   set progress completed steps to aFile
   
   -- Pause for demonstration purposes, so progress can be seen
   delay 0.05
end repeat

-- Reset the progress information
set progress total steps to 0
set progress completed steps to 0
set progress description to ""
set progress additional description to ""

It fails with an error message "File 1 wasn’t found." at the line "set aTitle to name of (info for aFile)" I'm sure it's because I am picking the files differently, but this would help so I have visualization of the progress of this routine. Can you help fix this (hopefully last) file selection question?


Steven Weyhrich <IX0YE><
MacBook, Mac OS X 10.15.1
Apple II History
http://apple2history.org

Offline

 

#15 2019-12-01 01:44:50 am

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

Re: Filename fun

You should repeat with counter i (integer number variable), then set a file to item i of files list. As here:

Applescript:


set theImages to choose file with prompt ¬
   "Please select some images to process:" of type {"public.image"} with multiple selections allowed

-- Update the initial progress information
set theImageCount to length of theImages
set progress total steps to theImageCount
set progress completed steps to 0
set progress description to "Processing Images..."
set progress additional description to "Preparing to process."

repeat with i from 1 to theImageCount
   set aFile to item i of theImages
   -- Update the progress detail
   set progress additional description to "Processing image " & i & " of " & theImageCount
   
   -- Process the image
   
   -- tell application "Finder" to set aTitle to name of aFile
   -- tell application "System Events" to set aTitle to name of aFile # Always consistent with the POSIX path !
   set aTitle to name of (info for aFile)
   set aPosix to quoted form of POSIX path of aFile
   
   display notification "posixPath : " & aPosix & linefeed & "fileName : " & aTitle
   -- get date and time from filename
   
   -- Increment the progress
   set progress completed steps to i
   -- Pause for demonstration purposes, so progress can be seen
   delay 1
end repeat

NOTE: No need reset the progress information, since you set progress information at the beginning of the script.

Last edited by KniazidisR (2019-12-01 01:51:07 am)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#16 2019-12-01 03:44:18 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3673

Re: Filename fun

You wrote:

Applescript:

repeat with aFile from 1 to length of theImages
set progress additional description to "Processing image " & aFile & " of " & theImageCount
set aTitle to name of (info for aFile)

which is wrong because in this case, aFile is not an alias but an integer

You have to choose between two ways to code the loop.

#1

Applescript:

repeat with i from 1 to length of theImages
# better with : repeat with i from 1 to theImageCount
set afile to item i of theImages
set progress additional description to "Processing image " & aFile & " of " & theImageCount
set aTitle to name of (info for aFile)

# 2 -- the one which I used

Applescript:

repeat with aFile in theImages
set progress additional description to "Processing image " & aFile & " of " & theImageCount
set aTitle to name of (info for aFile)

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 1 décembre 2019  10:43:15

Offline

 

#17 2019-12-01 04:18:55 am

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

Re: Filename fun

Yvan Koenig wrote:

You have to choose between two ways to code the loop.

#1

Applescript:

repeat with i from 1 to length of theImages
# better with : repeat with i from 1 to theImageCount
set afile to item i of theImages
set progress additional description to "Processing image " & aFile & " of " & theImageCount
set aTitle to name of (info for aFile)

# 2 -- the one which I used

Applescript:

repeat with aFile in theImages
set progress additional description to "Processing image " & aFile & " of " & theImageCount
set aTitle to name of (info for aFile)


Hi, Yvan. In the given case the OP should use only the 1st way, because he need one counter for progress bar indication.

Last edited by KniazidisR (2019-12-01 04:20:11 am)


Model: MacBook Pro
macOS Mojave -- version 10.14.4
Safari -- version 12.1
Firefox -- version 70.0

Offline

 

#18 2019-12-01 07:10:04 am

sweyhrich
Member
From:: Omaha, NE
Registered: 2003-11-21
Posts: 39
Website

Re: Filename fun

KniazidisR and Yvan, thanks for the correction, appreciate the help!


Steven Weyhrich <IX0YE><
MacBook, Mac OS X 10.15.1
Apple II History
http://apple2history.org

Offline

 

#19 2019-12-01 10:15:52 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3673

Re: Filename fun

KniazidisR wrote:
Yvan Koenig wrote:

You have to choose between two ways to code the loop.

#1

Applescript:

repeat with i from 1 to length of theImages
# better with : repeat with i from 1 to theImageCount
set afile to item i of theImages
set progress additional description to "Processing image " & aFile & " of " & theImageCount
set aTitle to name of (info for aFile)

# 2 -- the one which I used

Applescript:

repeat with aFile in theImages
set progress additional description to "Processing image " & aFile & " of " & theImageCount
set aTitle to name of (info for aFile)


Hi, Yvan. In the given case the OP should use only the 1st way, because he need one counter for progress bar indication.




Not really, the counter may be ruled an other way.

Applescript:

set theImages to choose file with prompt "Please select some images to process:" of type {"public.image"} with multiple selections allowed

-- Update the initial progress information
set theImageCount to length of theImages
set progress total steps to theImageCount
set progress completed steps to 0
set progress description to "Processing Images..."
set progress additional description to "Preparing to process."

set indx to 0 # Create a counter
repeat with aFile in theImages
   set indx to indx + 1 # increment the counter
   -- Update the progress detail
   set progress additional description to "Processing image : " & indx & " of " & theImageCount # display the counter
   
   -- Process the image
   
   -- tell application "Finder" to set aTitle to name of aFile
   -- tell application "System Events" to set aTitle to name of aFile # Always consistent with the POSIX path !
   set aTitle to name of (info for aFile)
   set aPosix to quoted form of POSIX path of aFile
   
   display dialog "posixPath : " & aPosix & linefeed & "fileName : " & aTitle
   
   -- get date and time from filename
   
   -- Increment the progress
   set progress completed steps to indx # use the counter
   
   -- Pause for demonstration purposes, so progress can be seen
   delay 0.05
end repeat

-- Reset the progress information
set progress total steps to 0
set progress completed steps to 0
set progress description to ""
set progress additional description to ""

Of course, the script must be saved as an application to make the progress bar visible.


I wish to explain the difference between Finder and System Events when extracting a filename.

Assuming that the file name displayed in Finder's window is 31/12/1943.png
which is not recommended but is perfectly correct (Apple itself use such names),
like info for, Finder will logically return : 31/12/1943.png
On its side, System Events will return : 31:12:1943.png
because its main setting is the UNIX one where levels in folders hierarchy are separated by / so this character can't be used in file/folder name.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 1 décembre 2019  17:13:14

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)