first applescript

Post Reply
danb
Newbie
Posts: 3
Joined: Tue May 10, 2016 1:01 am

first applescript

Post by danb »

I've written many applescripts and have been scripting for years. But never for Switch.

I have a script that will allow you to select and read a text file.
Then sets applescripts text item delimiters to a string I want to strip out.
Empties the text file and writes my resulting text items to it.

I want Switch to run my script on incoming file and pass it to next folder.

Here is my very simple script:
**********
choose file with prompt "Select the file you want to process:" without multiple selections allowed
set theFile to result
set TIDs to "<?xml version=\"1.0\"?>"

set x to read theFile -- read it all in
set AppleScript's text item delimiters to TIDs
set z to text items of x
set AppleScript's text item delimiters to ""
set y to text items of z as string

set eof of theFile to 0
write y to theFile
**********

Can someone send me a flow showing how this is accomplished? I hope to spring board from there.


thanks
User avatar
gabrielp
Advanced member
Posts: 577
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: first applescript

Post by gabrielp »

I know a few folks here use Applescript but the vast majority uses Javascript. If you're open to that, and are willing to expand on exactly what you want the script to do (particularly: "Then sets applescripts text item delimiters to a string I want to strip out. Empties the text file and writes my resulting text items to it."). I'd be happy to help you write this in JS.
Chat: open-automation @ gitter
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
danb
Newbie
Posts: 3
Joined: Tue May 10, 2016 1:01 am

Re: first applescript

Post by danb »

Excellent!

I want to strip <?xml version=\"1.0\"?> from an incoming text file and then forward edited text file.


that's it :)
User avatar
gabrielp
Advanced member
Posts: 577
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: first applescript

Post by gabrielp »

Off the top of my head, here is how I would do it. I adopted this script: https://github.com/open-automation/swit ... untRows.js

Split the file into array of lines, splice the array, removing the first line. Re-assemble into a single string, write to a new file, send the file out with a sendTo. Could be a simpler way of doing it but this is mine :D

Code: Select all

	
	// Helper function
	var forEach = function(array, callback){
   var currentValue, index;
   for (i = 0; i < array.length; i += 1) {
      if(typeof array[i] == "undefined"){
         currentValue = null;
      } else {   
         currentValue = array[i];
      }
      index = i;
      callback(currentValue, i, array);
    }
}
	// Construct File object
	var file = new File( job.getPath() );
	
	/// Open file as read only
	file.open( File.ReadOnly );
	
	// Create an array of the lines of the file
	var file_as_array = file.readLines();
	
	// Close the file
	file.close();
	
	
	// Get the first line
	// var first_line = file_as_array[0]; // This is how you get the first line if you need it
	
	// Slice the array without the first line
	var sliced_array = file_as_array.slice(1, (file_as_array.length-1));
	
	// Below this line is all psuedo code
	var new_file_string;
	forEach(sliced_array, function(line, index){
		new_file_string += line + "\n"; // Add each line to the string, one by one
	});
	
	var new_path = job.createPathWithName("whatever.txt");
	
	File.write(new_path, new_file_string, "UTF-8");
	
	// Send job to next flow element
	job.sendToSingle( new_path  );
Chat: open-automation @ gitter
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
freddyp
Advanced member
Posts: 413
Joined: Thu Feb 09, 2012 3:53 pm

Re: first applescript

Post by freddyp »

There are alternative approaches that do not require a Switch script.

Run an XSL transformation on the XML with the XSL below. The output setting will make sure that the resulting XML does not have the XML declaration line (it is scripting, Jim, but not as Switch knows it).

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output encoding="UTF-8" indent="yes" method="xml" omit-xml-declaration="yes"/>
    <xsl:template match="/">
       <xsl:copy-of select="/"/>
    </xsl:template>
</xsl:stylesheet>
Or, create a shell script:

Code: Select all

#!/bin/bash
tail -n +2 "$1" > "$2"
Do not forget to chmod +x the shell script file!! Select the shell script in "Execute command" with "Arguments" set to "$1" "$2" and "Output" to "File at path". Something similar can be concocted on Windows with a BAT file using "more +2", but I did not try that out.
Post Reply