Been a while since I was scripting and got stuck a bit with a script I need for automatic page layout. Hope somebody can help me with this one.
There’s a folder with picture files, all numbered and named like:
0001_FirstNameA_LastNameA.tif
0002_FirstNameB_LastNameB.tif
0003_FirstNameC_LastNameC.tif
And a folder with text files which could look like this:
0001_First NameA_Last NameA.txt
0002_First Name B_Last NameB.txt
0004_FirstNameC_Last NameD.txt
The list of text files can be slighty different than the one with picfiles, but the first four digits are leading to match picture and text.
Here’s what I got so far. I can create the two lists, get the first four characters of every item of the piclist.
But how do I get the right text file for each picture?
set picFolder to (choose folder with prompt "Select folder with images") as string
set picList to list folder picFolder without invisibles
set picListNums to {}
repeat with thisItem in picList
set end of picListNums to (characters 1 thru 4 of thisItem as string)
end repeat
set textFolder to (choose folder with prompt "Select folder with text files") as string
set textList to list folder (textFolder as alias) without invisibles
set picNum to item 3 of picListNums --just for testing I chose number 3.
set textFile to item of textList whose name starts with picNum -- and here it goes wrong.
{picList, textList, picListNums, picNum}
Clauses like whose doesn’t apply to AppleScript lists.
It’s written in AppleScript User Guide :
The filter form specifies application objects only. It cannot be used to filter the AppleScript objects list (page 112), record (page 118), or text (page 123). A term that uses the filter form is also known as a whose clause.
So, you must use a loop.
set p2d to path to desktop as text
--set picFolder to (choose folder with prompt "Select folder with images") as string
set picfolder to p2d & "thePictures"
set picList to list folder picfolder without invisibles
set picListNums to {}
repeat with thisItem in picList
set thisItem to contents of thisItem
set end of picListNums to ({text 1 thru 4 of thisItem, thisItem})
end repeat
--set textFolder to (choose folder with prompt "Select folder with text files") as string
set textFolder to p2d & "theTexts"
set textList to list folder (textFolder as alias) without invisibles
set choosed to 3
set picNum to item 1 of item choosed of picListNums --just for testing I chose number 3.
set textFile to "" # Must define the variable in case there is no match
repeat with aText in textList
if contents of aText starts with picNum then
set textFile to contents of aText
exit repeat
end if
end repeat
if textFile is not "" then
set thePictName to item 2 of item choosed of picListNums
{thePictName, textFile}
--> {"0003_tar_tempion.png", "0003_tar_tempion.txt"}
end if
--{picList, textList, picListNums, picNum}
Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) vendredi 25 septembre 2015 12:02:55
Thanks for you help. Tried to incorparate this in the bigger script, but without succes so far. It seems to go wrong in getting in the part of setting the picNum to item 1 of item eachfile.
Don’t get it, because when I fill in some item numbers the script comes back with results.
If you have some time to have a look at it again, much appreciate that.
Thanks,
Kjeld
set p2d to path to desktop as text
set picFolder to p2d & "thePictures"
set picList to list folder picFolder without invisibles
set picListNums to {}
repeat with thisItem in picList
set thisItem to contents of thisItem
set end of picListNums to ({text 1 thru 4 of thisItem, thisItem})
end repeat
set textFolder to p2d & "theTexts"
set textList to list folder (textFolder as alias) without invisibles
repeat with eachFile in picList
-- Action for importing the photo in the layout comes here.
set picNum to item 1 of eachFile of picListNums --Won't work with eachfile, but change to this and it works: set picNum to (item 1 of item 3) of picListNums
set textFile to "" # Must define the variable in case there is no match
repeat with aText in textList
if contents of aText starts with picNum then
set textFile to contents of aText
exit repeat
end if
end repeat
if textFile is not "" then
set thePictName to item 2 of eachFile of picListNums --Won't work with eachfile, but change to this and it works: set thePictName to item 2 of item 3 of picListNums
--{thePictName, textFile}
--> {"0003_tar_tempion.png", "0003_tar_tempion.txt"}
end if
end repeat
{picNum, aText, textFile}
Your instruction :
set picNum to item 1 of eachFile of picListNums –
means absolutely nothing.
Use a correct syntax and you will get a correct script.
set p2d to path to desktop as text
set picFolder to p2d & "thePictures"
set textFolder to p2d & "theTexts"
tell application "System Events"
set picList to name of files of folder picFolder whose visible is true
set textList to name of files of folder textFolder whose visible is true
end tell
set couples to {}
repeat with thePictName in picList
set thePictName to contents of thePictName
set picNum to text 1 thru 4 of thePictName
set textFile to "?????" # Must define the variable in case there is no match
repeat with aText in textList
if contents of aText starts with picNum then
set textFile to contents of aText
exit repeat
end if
end repeat
set end of couples to {thePictName, textFile}
end repeat
couples
--> {{"0001_pol_Ochon.png", "0001_pol_Ochon.txt"}, {"0002_truc_Muche.png", "0002_truc_Muche.txt"}, {"0003_tar_tempion.png", "0003_tar_tempion.txt"}, {"0004_lonesome_cowboy.png", "?????"}}
I revised the script because building the list picListNums was just wasting time.
I revised it again replacing calls to the deprecated “list files” by the recommended scheme using System Events.
Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) vendredi 25 septembre 2015 15:14:16
Thanks for your time and effort, man. As said before, it has been a while since I was scripting, so a lot of knowledge got lost in time, and gave me difficulties in making the correct loop. This goes way easier now with your revised script.
Just merged your script together with the rest and got it working now.