I know there’s a better way to do this than how I’m doing it…
set q to open for access file "Boot:Users:silversleeves:Desktop:path-test-1ln.txt"
set g to read q as text using delimiter (return)
close access file "Boot:Users:silversleeves:Desktop:path-test-1ln.txt"
Where I want to go from there, using what I have, is to trim the (return) off the one unbroken line of text in that file (formatted, itself, as an HFS path exported from iView Media Pro and cleaned up in Excel), then ‘feed it’ to an app like Preview.app or Goldberg – the oneliner is the path to a JPEG on another system disk not Boot.
Eventually, I’d like to be able to use the text file with all 23 items listed by path, and write a script that would display all of them at intervals via some app that doesn’t already have a slideshow feature – mainly to show the power of scripting in Mac OS X to Whoever.
Some advice as to any other ways to approach this?
Silversleeves
Model: PowerMac G4 QS 1.25 Dual MDD
AppleScript: 1.9.3
Browser: Firefox 1.5.0.8
Operating System: Mac OS X (10.3.9)
Ciao,
I would say it’s nearly impossible to propose anything without at least seeing an example of the text file you want to process…
a little bit more information ?
For openers, NB, you don’t have to open a file for access just to read it. This’ll do it.
set q to "Boot:Users:silversleeves:Desktop:path-test-1ln.txt" -- better as alias, except I don't have such a file.
set g to read q as text using delimiter (return)
As you’ve got it, that will get you a list of the paragraphs of the doc. so there won’t be any returns in it.
Don’t know what “the one unbroken line of text” refers to, though. After reading with a return delimiter, they’re all unbroken.
We might be much better off helping you if you give us a sample of the document you’re reading from (or if it’s confidential, make something up that’s representative).
“(formatted, itself, as an HFS path exported from iView Media Pro and cleaned up in Excel)” doesn’t help much - What did Excel do to it? Why aren’t you doing all that with your script - why pass it to Excel?
No problem. Here’s the text from the file “path-test-1ln.txt”
“Cleaning” the other one “up” in Excel was kind of incidental and arbitrary. The text file iView exported, thanks mostly to my not paying closer attention to what I was exporting (from “Make>Text data file”) included label headers and the file names as listed in the catalog I used to generate the list. Here’s one line from that file to illustrate.
Rather than trying to have the script ignore everything that came before the string(s) that resembled HFS paths to files, I used Excel to drop the whole first column as well as the header row. An old q&d workaround that was, looking back on it, wholly unnecessary.
set q to "Boot:Users:silversleeves:Desktop:path-test-1ln.txt" -- better as alias, except I don't have such a file.
set g to read q as text using delimiter (return)
gave me the error
So evidently it wants to read the file in in the old fashioned way (open for access)?
However, when it does it includes the carriage-return, instead of stopping the read-in even one character beffore it.
It won’t return “last character of g”, for instance, because returns (old ASCII 13’s) are of course non-printable.
Should I go in for a custom text delimiter “routine” – /set old to AppleScript’s/set AppleScript’s to (return), then later /set AppleScript’s to old again? (Here I’m using forward-slashes and italics to denote lines of script)
That couplet of lines Stefan gave in his last post,
worked insofar as AppleScript did not give the same error as before, except here’s the Result window output:
As you can see, it’s still reading in the carriage return instead of stopping with the “g” in “.jpg”. Which makes the string useless to pass as an alias to open the file in a graphics app.
Might it have something to do with the apps I’ve used to create or edit the text files? As I say, the 30-line original was created in iView MediaPro (which not too long ago was still happy writing text along SimpleText lines) and crunched in Excel (exported as tab delimited – maybe that’s where the problem is?). While I’m waiting for more educated guesstimations, I’ll open these files in an editor capable of showing non-printable markers (UnicodeChecker or BBEdit come to mind) and see if it’s not a TAB it’s choking on, instead of a (return).
Wish me luck.
Silversleeves
Quick Update - BBEdit Lite 6.1 shows a soft return, coincidentally just ahead of one of its own tab stops, at the end of the first line of the file “path-test-1iln.txt”. So the “using delimiter (return)” string in the script is either being wholly ignored or this isn’t the kind of RETURN AppleScript is accustomed to looking for.
Proof the string read in is worthless as a file path.
I added the following lines to the working facsimile of StefanK’s couplet (trying both “open file g” and the script code as below) to see if indeed the CR was still being read-in.
And running the script with these lines added gave this error :
The “open file g” gave an error a shade different – but along the same lines:
I went back to TextEdit, and backspace’d the CR off the end of the one-line text file, saved, and added this line to Stefan’s couplet:
I changed the variable g to t in the Goldberg “block” and ran the script. The JPEG referenced in the text file opened fine in Goldberg. But now I’m thinking, that’s just one line of text – the first text file had 30 lines, and as likely as not all of them were broken by the kind of CR that AppleScript (at least on my Mac) has proven time and again it prefers to read in as part of a given line rather than ignore (I have a sneaking suspicion it’ll prove me wrong on this, but I’m willing to bet it won’t). I may have to re-crunch the 30-liner in some app capable of converting Returns to Tabs, as I’m guessing there’s very little sunlight between what IVMP, TextEdit, Excel, etc, think a Tab is and how AppleScript looks at/for Tabs.
I’d love to see a way around having to re-crunch the longer text file, as it doesn’t bode well to have to take this extra step in the future with other file lists I may generate in iView. There’s still the option of re-defining AppleScript’s delimiters to something more in line with these ofther apps’, but I think it’d be like trying to force a horse to drink coffee – if AS isn’t inclined to see CR’s the same way as iView or Excel do, then there’s no amount of property redefinition that will make it do so, right?
I see your point – read the lines in as paragraphs. I’m thinking ahead a little to the original text file which had somewhere between 20 and 30 lines (all in the pattern, as I tried to say in my first post to this thread, of an HFS file path). Would I be able to pass those along with a repeat loop, like:
repeat from Swiss to (the number of items in item_list)
set Bern to item (Swiss) in (item_list)
tell application "Goldberg 2.5"
open Bern
end tell
wait 5
end repeat
I’m thinking I might have to, as this whole thing was to emulate/imitate slideshow-like capablility in apps that didn’t already have it.
Hoping you can help.
Silversleeves
PS: the variables were inspired by the fact that the author of Goldberg is in Switzerland.
set q to “Boot:Users:silversleeves:Desktop:path-test-1ln.txt” as alias
set g to paragraphs of (read q)
repeat with i in g
tell application “Goldberg 2.5”
activate
open (contents of i)
end tell
end repeat
I don’t know what Goldberg is. If the app that’s writing the file writes in utf16, then you might have to change it to utf8. But, since some strings of file paths work, The error is probably with the line endings I think.
Thanks. I like your suggestion. I’m trying, also, to work in a pause between closes and opens. In pursuit of that, I recorded Goldberg closing something, and came up with this:
It looks like, in order to close something, the name of the item to be closed has to be specified, as opposed to just saying “close window 1” or something as vague and general. Keeping that in mind, would your “(contents of i)” be too specific? From the script code above, I think I would just need a name, not a whole path, of the picture file to be closed before another one opens. Maybe calling info on “(contents of i)” and setting a variable for the name, then using that for the close command, would work?
‘contents’ just gets the value of a reference and I was afraid that it might add confusion. You probably need an alias reference, so rewrote it:
set q to "Boot:Users:silversleeves:Desktop:path-test-1ln.txt" as alias
set g to paragraphs of (read q)
repeat with i in g
set file_ref to i as alias
set n to name of (info for file_ref)
tell application "Goldberg 2.5"
activate
open file_ref
delay 5
close document n saving no
end tell
end repeat
Edited: now if you get an error with the references, then it could be that the file doesn’t exist. An alias reference checks to see if a file exists and errors if file not found.
set z to (text returned of (display dialog "Set your delay time (seconds): " default answer {"5"}) as integer)
set q to alias "Boot:Users:silversleeves:Desktop:path-test-1ln.txt"
set g to read q as text using delimiter (return)
set t to g as alias
set j to info for t
set namej to the name of j
tell application "Goldberg 2.5"
activate
open t
end tell
delay z
tell application "Goldberg 2.5"
close document namej saving no
end tell
…got me this Result:
Granted, I’m still using delimiters (not paragraphs as you suggested) to read in the path lines, which will break wildly when applied to any text file more than one line long, as we know. But the open, delay and close (of a specific doc by name, as Goldberg obviously wants) all work great at this stage. Now it’s time to test the “paragraph hypothesis” on the longer text file.
I tested the “paragraphs of” approach on a five-line edited version of the 20±line text file. It worked.
It occurs to me I could create some (correctly-exported) iView file lists this way, then add a “choose file with prompt” at the beginning, keep the dialog for setting delay time in seconds, and show any number of pics in Goldberg in sequence.
I’m such an iView maven, I have no idea at all if it’s the only cataloguer that exports text files according to user preferences. Does iPhoto, particularly the one in Tiger on the Cores, do anything similar to this? As the relative to whom I sent the email description of Goldberg is a dedicated iPhoto guy, I wanted to know if it would be possible to make this script adaptable to what his favorite catalog maker can export.