I have a script that reads my iCloud and pulls a certain file. This file is a .txt. File. Can I view that txt file as a html?
Here’s an example that writes some HTML to a .txt file and then reads it back in and displays it as HTML. The last line is the key for what you want.
//Text file for HTML
const textFilePath = "/var/mobile/Library/Mobile Documents/iCloud~dk~simonbs~Scriptable/Documents/htmlcontent.txt";
//Set-up a local file manager object instance
let fmLocal = FileManager.local();
//Write some test HTML
fmLocal.writeString(textFilePath, "<h2>Just a Heading</h2><ul><li><strong>BOLD</strong></li><li><em>italic</li><ul>");
//Read the file content and display it as HTML
WebView.loadHTML(fmLocal.readString(textFilePath));
Hope that helps.
It definitely helps!! Thank you! But what if the txt file is already in the iCloud directory?
The example just showed writing it as well to ensure it worked when it read it back in.
However given the sandboxing in iCloud, if you want to read a file outside of the Scriptable folder I think you have to browse for it (I get a’null’ content if I use the direct file URL) like this.
//Browse for a file
let utis = ["public.plain-text"];
let fileURLs = await DocumentPicker.open(utis);
//Get the file URL
let fileURL = fileURLs[0];
//Read the file content and display it as HTML
let fmLocal = FileManager.local();
WebView.loadHTML(fmLocal.readString(fileURL));
1 Like