Jump to content

newbie question: variables in Applescript


Recommended Posts

Is there a way to set values for a set of several variables inside an Applescript in a workflow ? I'm using a choose from list that induces different values for regex  replacements and would like to set these within the applescript as later on it's not easy to set different branches according to the outcome of the choice (or I don't find this easy)

Edited by jaladuvar
Link to comment

You'll have to be more specific if you want concrete help (i.e. post your workflow).

 

Here's how to set variables:

Fundamentally, it's not super-easy in AppleScript because you have to emit JSON. Using JavaScript (or basically any other language) would make it easier.

Link to comment

Basically I'm copying account  statement of different Bank cards (amex, visa, or big shops ) that need to be transformed into cvs files In ordre to import them into a personnel finance management software ( iCompta )

For a given card a set of regex replacement does the trick, but it differs whith each  type of cards.

i coule havé a Workflow for each card but I'd rather have one where I specify the card while it's running ( when copying the lines of the bank statement) and set the proper regex and other relevant variables ( In total 4 variables have  to be changed ) then.

Processing of the clipboard after copying

1/ regex

2/ replacement $1 etc differs

 

saving to file

 

3/ first Line ( column names and number ) of the file

4/ name of the file

 

I will attach a workflow later as I'm writing this on a iPad

Link to comment
15 minutes ago, vitor said:

 

Leave it to AppleScript to be so bad even JavaScript is a better recommendation.

 

Javascript the language isn't so terrible (although its community is pretty demented), but Apple's JXA API is in many ways worse than the AppleScript one.

 

@jaladuvar If I were you, I'd strongly consider doing the absolute minimum in AppleScript, and passing the data to a Ruby/Python/anything-but-AppleScript program as soon as possible. 

Link to comment

Here's an edited version. It doesn't work, but it shows you how to properly set variables.

 

I don't think the workflow can work unless you run it for each line of input separately. Alfred doesn't let you process multiple items at once, which is what you're trying to do (i.e. multiple transactions).

 

Realistically, I think you're going to have to write a script in PHP/Python/Ruby/JS to do the parsing, so you can handle multiple lines at once.

Link to comment

Thank you, it somewhat works with a few adjustments (only for Visa, as I have to refine the regex for other cards). Once I finalise it I will post it there for your final comments.

I tried parsing the lines in the HTML files with php or like but for a single "expense" sometimes the number of lines changes end only regex will make the trick. And I found Applescript (at the beginning) easier forme to handle.

When I copy a multi lines bank statement I get them all handled at once... 

 

 

Largeview.png

Link to comment
3 hours ago, jaladuvar said:

I tried parsing the lines in the HTML files with php or like but for a single "expense" sometimes the number of lines changes end only regex

 

PHP/Python etc. all support regular expressions.

 

3 hours ago, jaladuvar said:

When I copy a multi lines bank statement I get them all handled at once...

 

They are all passed into the script, yes. But your regular expressions will all only recognise one line.

Link to comment
On 24/06/2017 at 4:01 PM, deanishe said:

 

PHP/Python etc. all support regular expressions.

 

 

They are all passed into the script, yes. But your regular expressions will all only recognise one line.

 

I only face issue with the Amex card when the regex addresses several lines in the text (ie you'll find \n in the regex), for Visa, the text comprises several lines but the regex handles only one as it does not contain any  \n. Is this what you mean ?

I've put the regex in an Applescript in the workflow and it works (but I will have to change the complete architecture of the workflow).

 

I tried to get the different codes supplied through regexmatch (once established that the regex was working), but did not manage to get the relevant scripts to work...:wub:

Edited by jaladuvar
Link to comment
7 hours ago, jaladuvar said:

Is this what you mean ?

 

Not quite. What I mean is that your regexes only match one line (when the Amex one is fixed). So if you pass several lines into the script at the same time, only the first line will be matched. The rest will be ignored or passed through without being processed.

 

You need a script to split the input into multiple lines and loop through them. Alfred had no native ability to split a single input into several and handle each of them individually, which is what you need. Something like this:

import sys

query = sys.argv[1]  # equivalent to $1

lines = query.split('\n')  # separate text into individual lines


def process_line(line):
    """This function processes each line of input using regexes."""
    # Processing code goes here
    # ...
    # ...


transactions = []
for line in lines:
    processed = process_line(line)  # your function that handles each line
    transactions.append(processed)

print('\n'.join(transactions))  # combine parsed lines into single string and pass to next element

You could also pass each individual line back into the workflow via an External Trigger, so it is processed by workflow elements, not a script.

 

But in any case, I think you need a script to split the input into its individual lines and loop over them.

Link to comment

I must by stupid.

The Visa example I supplied is the following and all the lines were processed with the regex in one go

20/06/2017     1606/JARDIN DU CAP 06 SAINT LAURENT    -33,80 EUR
 19/06/2017     1606/LA POSTE BOUTIQU 75 PARIS CEDEX 15    -14,50 EUR
 19/06/2017     1606/ESSFLOREALYG820 83 FREJUS    -61,02 EUR

 

the structure of the Amex is as follows (I give only three "extended" lines and I keep the "internet" formatting ) and this does not work, but in the first instance I never specify that the text has to be splitted and it works

       
14 juin	  ITUNES.COM/BILL LUXEMBURG
Enregistrée le : 14 juin 
INTERNET TRANSACTION
21,99 EUR	 
16 juin	  APPLE ONLINE EURO HOLLYHILL
Enregistrée le : 17 juin
 	89,95 EUR
17 juin	  AMAZON EU SARL GUYANCOURT
Enregistrée le : 18 juin
 	16,80 EUR

 

Edited by jaladuvar
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...