Reading Contents of a File

Hi there,

Fairly novice Scriptable (and JavaScript!) user here. I’m trying to work on a widget that will show my a different different random line from a text file stored on iCloud. So far I’m stuck on just reading the contents of a file (actually, even just finding the file in Scriptable.) I think I’ve found the right path, and I’m 100% certain my file exists in that path with that name, but Scriptable is telling me it can’t find it. Any ideas?

function getFilePath() {
  let fm = FileManager.iCloud()
  let dirPath = fm.documentsDirectory()
  return fm.joinPath(
    dirPath,
    "dailyrem.txt")
}

let fm = FileManager.iCloud()
let path = getFilePath()
let raw = fm.readString(path)
let exists = fm.fileExists(path)

console.log ("Document path is "+fm.documentsDirectory())
console.log ("The file path that was build is "+getFilePath())
console.log ("Files exists? "+exists)
console.log (raw)

Here’s the output from the console:

2020-10-04 16:15:49: dailyrem.txt does not exist. Use fileExists(filePath) on a FileManager to check if a file exists.
2020-10-04 16:15:49: Document path is /private/var/mobile/Library/Mobile Documents/iCloud~dk~simonbs~Scriptable/Documents
2020-10-04 16:15:49: The file path that was build is /private/var/mobile/Library/Mobile Documents/iCloud~dk~simonbs~Scriptable/Documents/dailyrem.txt
2020-10-04 16:15:49: Files exists? false
2020-10-04 16:15:49: null
1 Like

Works fine for me.

Are you sure you have the file named correctly and in the right place?

i.e. in the Scriptable folder in iCloud.

That is really weird. Triple checked again. I had it placed in a folder called “Documents” in the Scriptable directory. I also tried just putting it in the Scriptable directory and no dice.

Gar, I figured it out. Listed contents of the folder and somehow I’d mistakenly named it “dailyrem.txt.txt”.

@tommertron, your post and the replies were helpful to me.

I’m working on a script to have Siri read aloud one random line from a .txt doc and to do so again and again, every ten seconds.

Would you be willing to post more of your script (part of it might be relevant, correct?) and/or point me toward threads that might help me? Thanks to anyone else, too!

I have the script successfully identifying the .txt file in iCloud. Thanks again for any input on what’s been useful to you.

Sure! Please don’t judge the code though. It’s not very well organized or documented, or probably even all that efficient. :slight_smile:


// Define where document is stored 
  
function getFilePath() {
  let fm = FileManager.iCloud()
  let dirPath = fm.documentsDirectory()
  return fm.joinPath(
    dirPath,
    "dailyrem.txt")
}

// Random number generator 

function getRndInteger(min, max) {
  return Math.floor(Math.random() * (max - min) ) + min;
}


// Define function to pick random quote 
function displayQuote () {
  let fm = FileManager.iCloud()
  let path = getFilePath()
  let raw = fm.readString(path)
  let quotes = raw.split("\n")
  let lines = quotes.length
  let pickQuote = getRndInteger(2, lines);
  // the following lines can be uncommented for troubleshooting 
  //console.log("Number of lines is "+lines)
  //console.log ("Picking array number "+pickQuote)
  //console.log(quotes[pickQuote])
  let result = quotes[pickQuote]
  return result
}

//console.log (displayQuote())

let widget = new ListWidget()

displayQuote.textColor = Color.white() 
widget.addText(displayQuote())
 
// Finalize widget settings
//widget.setPadding(16,16,16,0)
//widget.spacing = -3
// Set gradient background
let startColor = new Color("FF6F91")
let endColor = new Color("FF9671")

let gradient = new LinearGradient()
gradient.colors = [startColor, endColor]
gradient.locations = [0.5, 1]
widget.backgroundGradient = gradient

Script.setWidget(widget)
widget.presentMedium()
Script.complete() 

1 Like