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:
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.
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.
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
Maro - questions about your code. Tell me if I understand this correctly:
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
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
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 at the start of it. The different folders are separated by “:”
repeat with i in aList
set aFile to (i as alias)
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!!
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?
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.
I am a bit puzzled by what you report but I think that I found the explanation.
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
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"?
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 :
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
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.
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.
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
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.
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:
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?
You should repeat with counter i (integer number variable), then set a file to item i of files list. As here:
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.
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
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
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
Not really, the counter may be ruled an other way.
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