AutoControl Control your browser your way Forum               Install now from theChrome Web Store

ACtl.saveFile


Saves data to a local file in various formats. The maximum size of the data is only limited by the available RAM and disk space. ACtl.saveFile(filePath,content[,options]) filePath Type: string Local path or UNC path of the destination file. It can be relative to the script's location (if there is one). It can also contain placeholders such as <desktop>, <documents> and others.
If the file doesn't exist, the entire path is created.
content Content to save to the file. It can be any of the types below. Each type will be serialized as follows:
string The text enconded as specified by options.format.
binary string
Blob
File
ArrayBuffer
The exact byte sequence.
Element
DocumentFragment
The HTML code of the element, enconded as specified by options.format.
HTMLImageElement
SVGImageElement
HTMLCanvasElement
OffscreenCanvas
The image content formatted as the file extension in filePath, e.g. png, jpg (this can be overriden by options.format).
Object The JSON representation of the object.

Any other type will be converted to a string and saved as is.
options Type: Object Default: {} An object with one or more of the following properties: append Type: boolean Default: false Append to, instead of replace, the file's existing content. format Type: string Default: 'UTF-8' | 'png' For text, any of 'UTF-8', 'UTF-16LE' or 'UTF-16BE'.
For images, any of 'png', 'jpg' or 'webp'.
quality Type: number Default: 0.9 When format is 'jpg' or 'webp', this gives the image quality as a number between 0 and 1.
Returns Type: Promise Resolves to: string Returns immediately. The Promise will resolve with the absolute file path to which content was saved. Throws Type: string Error description if filePath cannot be accessed or if content cannot be serialized.

Examples

Replace all occurrences of one word with another in a text file.

	let filePath = '<desktop>/textFile.txt' ;
	let find = 'John', replace = 'Jane' ;
	//Get the file content
	let textContent = await ACtl.getFile(filePath) ;
	//Replace all ocurrences of `find` (case-insensitive)
	textContent = textContent.replace(RegExp(`\\b${find}\\b`,'ig'), replace) ;
	//Save the new content back to the file
	await ACtl.saveFile(filePath, textContent) ;
	

Save settings to a JSON file.

	let filePath = '<desktop>/scriptSettings.json' ;
	let mySettings = {
		setting1: 'Lorem ipsum',
		parameter2: 12345,
		option3: true
	} ;
	await ACtl.saveFile(filePath, mySettings) ;
	

Download a ZIP file and unzip it to disk on the fly.

	//Include the JSZip library
	await ACtl.include('https://cdnjs.cloudflare.com/ajax/libs/jszip/3.2.2/jszip.min.js') ;
	//Get the binary contents of the .zip file into a variable
	let zipFile = await ACtl.getFile('http://autocontrol.app/tests/zipTest.zip', 'binary') ;
	//For each file item in the zip container
	( await JSZip.loadAsync(zipFile) ).forEach( async (path, zipObj)=>{
		//Ignore folder items
		if(zipObj.dir) return ;
		//Obtain the file's uncompressed content
		let blob = await zipObj.async('blob') ;
		//Save the file content to its corresponding subfolder under the desktop
		await ACtl.saveFile('<desktop>/zipTest/' + zipObj.name, blob) ;
	}) ;
	
Scripting API Script Isolation Asynchronicity Backgrnd Scripts GUI vs API ACtl.include ACtl.import ACtl.getFile ACtl.saveFile ACtl.saveURL ACtl.openURL ACtl.closeTab ACtl.runInTab ACtl.runInFrames ACtl.runInPageCtx ACtl.getTabInfo ACtl.getTabIds ACtl.TAB_ID ACtl.setTabState ACtl.captureTab ACtl.execAction ACtl.runCommand ACtl.getClipboard ACtl.setClipboard ACtl.expand ACtl.switchState ACtl.var ACtl.pubVar ACtl.on ACtl.off ACtl.sleep ACtl.STOP_CHAIN ACtl.STOP_FULL_SEQ