0

I have a list called num. I have converted it into a string. I want to return that string so that the next action called "New text file" in my automator workflow can get the string. This is my code -

on run {input, parameters}
    set num to {1,2}
    set f to ""
    repeat with x in num
        set f to f & x & "\n"
    end repeat
    return f
end run

It doesn't return anything.

EDIT: As some of you have asked. I am going to elaborate on the question. My automator workflow, takes some parts of some text in an email, it retrieves all the phone numbers in the group which is specified in the text of the email, and stores them in a text file.

the variable 'cat' has the list of contact groups

This is the full code:

on run
    set cat to value of variable "cat" of front workflow (*I have already defined the variable elsewhere in the workflow*)
    set num to {}
    set f to ""
    tell application "Contacts"
        repeat with i in cat
            set inGroup to group i
            set phoneProps to value of phones of inGroup's people
            set end of num to first item of first item of phoneProps
        end repeat
    end tell
    repeat with x in num
        set f to f & x & "\n"
    end repeat
    return f
end run

This code is supposed to be followed by a 'new text file' action which should take the output of the previous action ('run applescript') as input. That doesn't happen as for some reason, applescript refuses to return the value even when I am getting the desired value when using 'display dialog'.

UPDATE: I am now posting the entire workflow

Input- In mail app - An email with this content - "#a, #b Q: this is a question for all of you."

Workflow -

1) Run Applescript Code -

 on run {input, parameters}

    tell application "Mail" to set theMessageText to content of (get first message of inbox)
    set topic to text ((offset of "#" in theMessageText) + 1) thru ((offset of "Q" in theMessageText) - 1) of theMessageText
    set AppleScript's text item delimiters to ", "
    set bowords to words of topic
    set o to length of bowords
    repeat with i from 1 to o
        trim_line(bowords, "#", 0)
    end repeat
    bowords
end run
on trim_line(this_text, trim_chars, trim_indicator)
    set x to the length of the trim_chars
    -- TRIM BEGINNING
    if the trim_indicator is in {0, 2} then
        repeat while this_text begins with the trim_chars
            try
                set this_text to characters (x + 1) thru -1 of this_text as string
        on error
                -- the text contains nothing but the trim characters
                return ""
            end try
        end repeat
    end if
    -- TRIM ENDING
    if the trim_indicator is in {1, 2} then
        repeat while this_text ends with the trim_chars
            try
                set this_text to characters 1 thru -(x + 1) of this_text as string
            on error
                -- the text contains nothing but the trim characters
                return ""
            end try
        end repeat
    end if
    return this_text
end trim_line

2) Set Value of Variable

Sets the output of previous action to 'cat'

3) Run Applescript

on run {input, parameters}
    tell application "Mail" to set theMessageText to content of (get first message of inbox)
end run

4) New Mail Message

Appends the text from previous action to the bod of email

5) Run Applescript

The code that @Tetsujin gave in the answers

6) New Text File

7) Add attachments to front message

8) Send Outgoing Messages

9) Run Applescript (Deletes the email)

Code -

on run {input, parameters}
    delay (10)
    tell application "Mail"
        set theMessages to (get selection)
        repeat with eachMessage in theMessages
            set theAccount to account of (mailbox of eachMessage)
            move eachMessage to mailbox "Trash" of theAccount
        end repeat
    end tell    
    return input
end run
12
  • 1
    num is a list. you need to count num to be able to iterate it. also likely to need to ask for x as string [sorry, my brain is working in another language at the moment, so you may have to translate my terminology back to applescript]
    – Tetsujin
    Commented Jan 16, 2015 at 19:56
  • My guess is that either the input is never reaching the Applescript or that it's not in a format your script can use. If you remove the {input, parameters} and run the code in Applescript Editor, it spits out 1\n2. You should say exactly what you're trying to automate and what you've done so we can fully understand the problem. Commented Jan 16, 2015 at 21:02
  • Your code returns "1 2, In Automator if you run it there. (The 2 being on a newline, but cannot format in this comment) " Also @Tetsujin , I am pretty sure because f is declared as a string with "" and then proceeds the variable x the result will be a string.
    – markhunte
    Commented Jan 17, 2015 at 1:05
  • I have updated the question @JayThompson
    – Samyak
    Commented Jan 17, 2015 at 4:25
  • I have updated the question @markhunte
    – Samyak
    Commented Jan 17, 2015 at 4:25

1 Answer 1

1
on run
    set cat to value of variable "cat" of front workflow (*I have already defined the variable elsewhere in the workflow*)
    set num to {}
    set myString to ""
    tell application "Contacts"
        repeat with i in cat
            set inGroup to group i
            set phoneProps to value of phones of inGroup's people
            repeat with i in phoneProps
                try
                    set myString to myString & "\n" & first item of i (*gets first number only*)
                on error
                    set myString to myString & "\n" & "blank field" (*covers for empty phone number, otherwise would halt on error*)
                end try
            end repeat
        end repeat
        return myString
    end tell  
end run
13
  • In the 'run applescript' action, the result shows blank even for the above code. @Tetsujin
    – Samyak
    Commented Jan 17, 2015 at 15:35
  • if the result is empty, then your input is empty, or you're not carrying the result over to the next stage correctly. Works perfectly here. I changed set cat to {"oneGroup", "anotherGroup"} from my own contacts list. return myString shows all first-listed phone numbers & one 'blank field'
    – Tetsujin
    Commented Jan 17, 2015 at 15:39
  • When I use 'display dialog', I see the value of myString correctly. Only when I return it, it does not show anything in he result of the 'run applescript' action. For a couple of times, the result mysteriously shows correctly, other times it does not, even when I do no change to the workflow. @Tetsujin
    – Samyak
    Commented Jan 17, 2015 at 15:55
  • first time i've every tried automator - I wrote that just in Script Editor - I dropped a 'run applescript', pasted that in, added a 'new text file' after it & it works just fine, new file on my desktop with a list of phone numbers. I am not using any input in 'on run' i.e. no {}
    – Tetsujin
    Commented Jan 17, 2015 at 16:05
  • Like I said, it works sometimes, but then later, it doesn't. Even after, I have apparently changed nothing. Can you thing of any possible things I could be doing wrong? @Tetsujin
    – Samyak
    Commented Jan 17, 2015 at 16:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .