I have a problem with this script

tell application "Finder"

-- set random_file to some file of folder "titre" in desktop

set random_file to file "rds_espace.txt" of folder "titre" in desktop

duplicate random_file to folder "Nicecast" in desktop with replacing

set name of some file of folder "Nicecast" in desktop to "NowPlaying.txt"

-- set name of some file of folder "Wintec SSD/Users/vincentm/Library/Application Support/Nicecast" to "NowPlaying.txt"

end tell


Posted on May 5, 2021 6:48 AM

Reply
Question marked as Top-ranking reply

Posted on May 7, 2021 11:29 AM

> It works, however there is an error at times on "text item 2 of" <html> ". (-1728).


Check the content of the files.


Based on the original request, the script 100% assumes that the first line of the file contains two pieces of text separated by a '-' symbol, such as:


The Best Song in the World - Super Duper Band


The script simply splits the text at the "-" symbol and refers to the text before it (i.e. "The Best Song in the World") as theTitle, and the text after it (in this case "Super Duper Band") as theArtist.


That's it. Nothing more.


The fact that it's complaining it "cannot get text item 2 of "<html>" " tells me that the file is not a plain text file that follows this format. Instead it sounds like a HTML file. The script has no expectation or understanding of how to handle HTML files.


So, edit the file so that the data is in the format expected, or change the requirements so that the script can more intelligently extract the data from the file.

12 replies
Question marked as Top-ranking reply

May 7, 2021 11:29 AM in response to vincentm27

> It works, however there is an error at times on "text item 2 of" <html> ". (-1728).


Check the content of the files.


Based on the original request, the script 100% assumes that the first line of the file contains two pieces of text separated by a '-' symbol, such as:


The Best Song in the World - Super Duper Band


The script simply splits the text at the "-" symbol and refers to the text before it (i.e. "The Best Song in the World") as theTitle, and the text after it (in this case "Super Duper Band") as theArtist.


That's it. Nothing more.


The fact that it's complaining it "cannot get text item 2 of "<html>" " tells me that the file is not a plain text file that follows this format. Instead it sounds like a HTML file. The script has no expectation or understanding of how to handle HTML files.


So, edit the file so that the data is in the format expected, or change the requirements so that the script can more intelligently extract the data from the file.

May 6, 2021 11:20 AM in response to vincentm27

> I have a folder called "title" which is a volume window where .txt files are generated


OK. Got that.


> there are only 2 texts separated by a -


Is this the file name? or is this the contents of the text file? It isn't clear. Since you say '... on one line' that makes me think this is the contents of the file. Nowhere in your original request did you talk about having to extract data from the text file.


IF the folder contains a number of text files, each containing something like "xxxxxxx - yyyyyyy" then I think what you're asking for is:


  1. select some random file from the "title" folder
  2. read the contents of the file (e.g. "xxxxxxx - yyyyyyyy")
  3. determine the song title ("xxxxxxx") and artist name ("yyyyyyy") from the file contents
  4. write this data to the "NowPlaying.txt" file in the form:


Title: xxxxxxx

Artist: yyyyyyy


That's pretty straightforward:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "Finder"
	-- step 1: select a random file
	set random_file to (some file of folder "title" of desktop) as alias
	-- step 2: read the first line of the file contents
	set file_contents to paragraph 1 of (read random_file)
	-- step 3: break out the artist and track names
	set {theTitle, theArtist} to my getTrack(file_contents)
	-- step 4: reconstruct the data in the required format
	set nowPlayingData to "Title: " & theTitle & return & linefeed & "Artist:" & theArtist as text
	-- and write it to the file
	set npFile to open for access ((file "NowPlaying.txt" of (folder "Nicecast" of (get path to application support from user domain))) as alias) with write permission
	-- clear the existing file content
	set eof npFile to 0
	-- write the new data
	write nowPlayingData to npFile
	-- and close the file
	close access npFile
end tell

on getTrack(theText)
	set {oldTID, my text item delimiters} to {my text item delimiters, "-"}
	set w1 to text item 1 of theText
	set w2 to text item 2 of theText
	set my text item delimiters to oldTID
	return {w1, w2}
end getTrack


Note that since you are not moving the file, but re-writing its contents, I opt to simply overwrite the existing file contents with the new data. Your original request was just to move the random_file into place, but that does not format the data into the required format.

May 5, 2021 10:04 AM in response to vincentm27

Maybe I'm missing something, but it's 100% unclear to me as to what you're asking.


You have a script. OK. So what?


I assume you're having some problem with the script in that it's not doing what you expect.


You don't explain what it is you're hoping to achieve. You don't say what isn't working in your script right now. For example, when the script fails it likely throws an error message and highlights the offending line in the script. Identifying this line and stating the error message will go 99% of the way to understanding what's wrong.


As it is no one can replicate your script because our disk layouts are different. We may not have the same folders or folder content, so any errors we see may not be the same ones you get (for example, I get an error that there is no folder "titre" on my desktop, which makes total sense. Sure, I can create this folder, or change the script to point to a folder that I do have, but that changes things.


So let's step back a bit. Start by explaining what it is you're trying to do. Follow that with your script, along with an explanation of where it's failing (what it is/is not doing; error messages, etc.). Then it should be easy for someone to help you.

May 5, 2021 2:34 PM in response to vincentm27

> In fact I have 1 folder called "title" of which there is a .txt file...


Is the folder called "title", or "titre"? Because it matters to the script.

Do you want a random file from this folder (as hinted by the name of your variable random_file)? or a specific file? (your script always picks the file called "rds_espace.txt")


> and to finish it would be necessary that the text file be modified before renaming it, namely I have 2 words to add "Title: with the 1 word" of the duplicated text and "Artist: with the 2 word


Nowhere in your script do I see your 'word 1' and 'word 2' that you want to rename the file to. Where do these words come from?


May 6, 2021 1:49 AM in response to Camelot

I have a folder called "title" which is a volume window where .txt files are generated where there are only 2 texts separated by a - this text represents the name of a song and the name of the artist on one line (xxxxx - xxxxx) so this file is not compatible with the software that I use namely Nicecast which wants to consult every 15 "a file which is named" NowPlaying.txt "which is formatted in ( Title: xxxxx) and line below (Artist: xxxxx)

On the other hand, the Nicecast folder is located in the library, the user's support application.

May 14, 2021 11:28 AM in response to vincentm27

Hello,

Here is the final script, I do not know if we can simplify it because it must work 24 hours a day, therefore interrogate the text file concerned.


use AppleScript version "2.4" -- El Capitan (10.11) or later

use scripting additions


tell application "Finder"

with timeout of 86400 seconds

-- step 1: select a random file

set random_file to (file "rds_espace.txt" of folder "titre" of desktop) as alias

-- step 2: read the first line of the file contents

set file_contents to paragraph 1 of (read random_file)

-- step 3: break out the artist and track names

set {theArtist, theTitle} to my getTrack(file_contents)

-- step 4: reconstruct the data in the required format

set nowPlayingData to "Title: " & theTitle & return & linefeed & "Artist:" & theArtist as text

-- and write it to the file

set npFile to open for access ((file "NowPlaying.txt" of (folder "Nicecast" of (get path to application support from user domain))) as alias) with write permission

-- clear the existing file content

set eof npFile to 0

-- write the new data

write nowPlayingData to npFile

-- and close the file

close access npFile

end timeout

end tell


on getTrack(theText)

set {oldTID, my text item delimiters} to {my text item delimiters, "-"}

set w1 to text item 1 of theText

set w2 to text item 2 of theText

set my text item delimiters to oldTID

return {w1, w2}

end getTrack


tell application "WinMedia" -- the name I gave to the original script

repeat

activate

delay 2

quit

delay 4

end repeat

end tell


Regards


Vincent M

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.

I have a problem with this script

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