Line breaks in string

I am working on a script that is capable of saving a guitar tab from a url into a pdf in iCloud. I am able to get the tab itself as a string but it contains line breaks ( “\n” ) that do not occur when saving the string into a file.

Anyone got to deal with these type of issue before?

Here’s my code:

let url = ""
let req = new Request(url)
let html = await req.loadString()

let tabStart = html.indexOf('{"wiki_tab":{"content":"')
let tabEnd = html.indexOf('","revision_id":',tabStart+1)

let tab = html.substring(tabStart+24,tabEnd)

const fileManager = FileManager.iCloud()
const outputPath = fileManager.joinPath(fileManager.documentsDirectory(),'/tab.txt')

fileManager.writeString(outputPath, tab)
1 Like

I’m confused as to what the issue is. Are you saying it doesn’t save because of the line break, or what exactly is happening and what do you want to happen?

incidentally, have you tried to do a .replace() on the string to remove the line break?

What I want is for the line breaks to happen when saving the string, not to stay inside the string as “\n”.

I am new to JS so I have no idea how JS handles strings. I have just tried .replace() but my ending string still has the line breaks.

Right now, I’m not sure if this is a bug caused by Request.loadString() or if there’s something fishy with the content on Ultimately Guitar. I’ll take a look at that.

This should work for now:

tab = tab.replace(/\\n/g, "\n")
1 Like

Awesome! That works, thank you Simon! I was just using on backward slash, that’s why it wouldn’t work for me.

Okay, so Ultimate Guitar has a string in a JSON object that contains the “\n” string and we want to replace that with an actual line break. Therfore I don’t think this is a bug in Scriptable nor Ultimate Guitar. This is the expected behaviour.

Thank you @simonbs! Your suggestion works like a charm so I can keep working on the script. Will share it once I’m done :wink: