using delimiter "tab"

I am trying play a list of songs thru afplay; the song list is ready as a TextEdit file with the paths to songs separated by tabs.
The following script is being tested in the script editor
====================
set song_file to "TextEdit file with paths to songs separated by tabs"
set fs to open for access song_file
repeat
set the_path to read fs as next using delimiter "tab"
if the_path is false then
exit repeat
end if
set posix_path to quoted form of the_path & "mp3"
do shell script ("afplay " & posix_path)
end repeat
close access fs
================
It works with 'read as text until ".mp3", but not all songs end with 'mp3' and it creates other problems.
I want to read a path with "tab" as delimiter, but it won't work.
Perhaps I should read the path as a paragraph, but I don't know how to effect this.
Moreover, this script works in the scripteditor, but not as 'perform Applescript' in my application, i.c FMP.

iMac 3.06, Mac OS X (10.6.3)

Posted on Jun 26, 2010 11:29 AM

Reply
8 replies

Jun 26, 2010 12:16 PM in response to apwscript

I want to read a path with "tab" as delimiter, but it won't work.


Sure it will - you're just doing it wrong 🙂

set the_path to read fs as text using delimiter "tab"


Your mistake is that you're quoting "tab". Since it's quoted it is treated as a literal string, so AppleScript is looking for the sequence of characters t-a-b to delimit the text.

Instead you want to use the keyword tab (with no quotes). The keyword identifies the tab character which will properly delimit your text as you expect:

set the_path to read fs as text using delimiter tab

Jun 26, 2010 12:23 PM in response to apwscript

You didn't mention what kind of paths are in your text file (the file needs to be plain text, otherwise the formatting will get in the way), but AppleScript's text item delimiters can break up the text at the specified delimiter (paragraphs are separated by returns, so there is no need to use that unless paragraphs are also used as delimiters). Reading the entire contents and working with the results does make things a little easier, such as not worrying about the end of the file - you also do not need to use *open for access* or close for files that you are just reading.

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
font-weight: normal;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px;
color: #000000;
background-color: #DAFFB6;
overflow: auto;"
title="this text can be pasted into the AppleScript Editor">
set songFile to "some plain text file with tab sepatated paths"

set theSongs to read file songFile -- get file text
set tempTID to AppleScript's text item delimieters -- save current delimiters
set AppleScript's text item delimiters to tab
set theSongs to text items of theSongs -- convert text to list at the delimiters
set AppleScript's text item delimiters to tempTID -- restore delimiters
log theSongs

repeat with aSong in theSongs
do shell script "afplay " & quoted form of POSIX path of aSong -- Finder path
do shell script "afplay " & quoted form of aSong -- POSIX path
end repeat
</pre>

Jun 26, 2010 2:49 PM in response to Camelot

Thanks for your reaction.

tab doesn't give the desired result.

I created the tab-separated text file in FileMaker Pro through 'export records'; according to the help information it is plain text. However, it says also, that 'the carriage return character separates records'.
It seems then, that the delimiter should be the carriage return. Is there a keyword for carriage return?

Jun 26, 2010 4:15 PM in response to red_menace

Thanks for your reaction.

The file I created in FileMaker Pro is a plain text, tab-separated text format. Each record consists of one variable/field.
Help info says, that 'the tab character separates fields, the carriage return character separates records'.

I ran your script and it gives an end of file error (-39) at the line 'set theSongs to read file song_file'.

Jun 26, 2010 4:53 PM in response to apwscript

If there aren't any tabs in there, then you don't need the text item delimiters stuff and can just get the paragraphs:
set theSongs to paragraphs of (read file songFile)
otherwise you can add delimiter characters, for example:
set AppleScript's text item delimiters to {tab, return, linefeed}

Error -39 is end-of-file, although there shouldn't be an error there. What does the text file look like from another application such as TextEdit?

Jun 26, 2010 10:49 PM in response to apwscript

Hello

According to your description, FMP generates a tab-separated-value file, where each field is separated by tab and each record separated by carriage return (CR).
If there's only one field (song path) in each record, the exported text consists of paragraphs each of which corresponds to a song path.

So you may try any of the following codes.
CODE1 is the simplest. AppleScript's paragraph is smart enough to handle all CR, LF, CRLF as delimiters.
CODE2 uses 'using delimiter' parameter of read command.
CODE3 spells out every step to open, read, close in addition to using 'using delimiter' parameter of read command.


--CODE1
set song_file to "HFS:path:to:song_paths.txt"
set pp to paragraphs of (read file song_file) -- System's primary encoding, e.g., Mac-Roman
--set pp to paragraphs of (read file song_file as «class utf8») -- UTF-8
repeat with p in pp
set p to p's contents
if (count p's words) > 0 then -- to skip quasi-blank line, just in case
do shell script "afplay " & quoted form of p
end if
end repeat
--END OF CODE1

--CODE2
set song_file to "HFS:path:to:song_paths.txt"
set pp to read file song_file using delimiter return -- System's primary encoding, e.g., Mac-Roman
--set pp to read file song_file as «class utf8» using delimiter return -- UTF-8
repeat with p in pp
set p to p's contents
if (count p's words) > 0 then -- to skip quasi-blank line, just in case.
do shell script "afplay " & quoted form of p
end if
end repeat
--END OF CODE2

--CODE3
set song_file to "HFS:path:to:song_paths.txt"
try
set fref to open for access file song_file
set pp to read fref using delimiter return -- System's primary encoding, e.g., Mac-Roman
--set pp to read fref as «class utf8» using delimiter return -- UTF8
close access fref
on error errs number errn
try
close access file song_file
end try
error errs number errn
end try
repeat with p in pp
set p to p's contents
if (count p's words) > 0 then -- to skip quasi-blank line, just in case.
do shell script "afplay " & quoted form of p
end if
end repeat
--END OF CODE3


*If the text is encoded in UTF-8, read the file as such.
Statement to read the fie as UTF-8 is currently commented out.

*As shown in CODE3, if you open access for a file, you must make sure you close access for it. Failing to close access would result in subsequent error to read the same file in the same script session, i.e. in the same session of Script Editor or Applet execution. I'd suspect your error -39 would be caused by not closing access for the file in the previous run.
To close all access for files which are inadvertently left open, you may quit the current script session, i.e., current application, e.g., Script Editor.

Good luck,
H

Message was edited by: Hiroto

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

using delimiter "tab"

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.