Processing list of selected text in automator with applescript - applescript

While trying to make an automator action that opens multiple tabs from selected text as input I ran into an applescript issue I wasn't able to solve for awhile. This includes the answer and I'm posting this here because I just wasn't able to find documentation on how to handle data in "input" for a receives selected "text" in "any application" automator action, everything is for files which comes in as a list already.
When putting an applescript action in, you get:
on run {input, parameters}
the problem here is that input isn't in a list format and trying to do anything with it breaks the script or throws an error. ie I can't do:
repeat with URL in input
set this_URL to URL
So how can I treat a list of selected text as a list of items?

the solution is first treat input as a string then break apart every paragraph.
on run {input, parameters}
set inputText to input as string
set URL_list to every paragraph of inputText
Without treating input "as string" first before doing "every paragraph of" it won't work.
Here's the end working script, replace the "some_url" with your own. You'll be able to select several lines of text in an editor and treat each one as a parameter to your fixed url opening each in a new safari tab. This could be expanded upon by having each line be delimited for multiple params on the url.
on run {input, parameters}
set inputText to input as string
set URL_list to every paragraph of inputText
tell application "Safari"
activate
repeat with URL in URL_list
set this_URL to URL
# extra processing of URL could be done here for multiple params
my new_tab()
set tab_URL to "http://some_url.com?data=" & this_URL
set the URL of document 1 to tab_URL
end repeat
end tell
return input
end run
on new_tab()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "New Tab" of ¬
menu "File" of menu bar 1
end tell
end tell
end new_tab
As an example say you had the list and had a service of the above using "http://stackoverflow.com/posts/" & this_URL
6318162
6318163
6318164
you could now select them click services and choose your "StackOverflow - view questions" service and it'll append and open each one in a new safari tab. In my case I needed to verify multiple dns entries in our server as still valid and do a bunch of whois lookups.

I was looking for the same thing, just for files as input from Automator to AppleScript.
ddowns's trick didn't work for that, but ended up using this, hope it's helpful for someone looking for solving the same issue I ran into:
on run {input, parameters}
-- create empty list
set selectedFiles to {}
-- add each list item to the empty list
repeat with i in input
copy (POSIX path of i) to end of selectedFiles
end repeat
-- show each item (just for testing purposes of course)
repeat with currentFile in selectedFiles
display dialog currentFile as text
end repeat
end run

As Hanzaplastique says, for AppleScript within Automator, you don't need the Safari AppleScript because there's an Action for that. I use the following Actions:
Extract URLs from Text (actually the 'Extract Data from Text' Action)
Run AppleScript
Display Webpages
I use it as a Workflow added to the Services menu so that I can right-click on selected text in an email and open multiple URLs in Safari tabs.
In particular, I get Server / WordPress updates in an email but the URLs are just the top level of the domains and I want to jump to the plugins page of WordPress. So, my AppleScript (with thanks to Hanzaplastique) is:
on run {input, parameters}
set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}
-- create empty list
set selectedFiles to {}
-- add each list item to the empty list
repeat with i in input
set AppleScript's text item delimiters to {" "}
set i to i & "/wp-admin/plugins.php"
copy i to end of selectedFiles
end repeat
return selectedFiles
end run
I found I needed the 'return selectedFiles'. The always mysterious (to me) text delimiters may not be necessary and come from the previous version which only pulled out a single URL.

Related

How do I make a Pop-Up Menu from my Contacts Group names, using AppleScript and Numbers?

Please note: The site is not letting me post this question unless I incorrectly format it i.e. the two middle sections of "code" which aren't actually code. I've done my best to edit it, to fix it, and I apologise if the formatting is poor.
I’ve found and adjusted two scripts, which run, individually. This is my first AppleScript project, and I’d like help joining them together to MAKE ONE SCRIPT.
I want Script 2 to use the result from Script 1.
e.g. set menuItems to (Script 1 Result names)
I made a dummy testGroup in my Contacts. The Pop-Up Menu only needs the names. I will use the corresponding email addresses later, in another script.
I hope this is clear enough. Thanks in advance for your help.
Script 1
--returns Contacts testGroup’s names and email addresses
set myList to ""
tell application "Contacts" to repeat with p in people in group "testGroup"
if emails of p is {} then
set e to ""
else
set e to value of email 1 of p
end if
set myList to myList & name of p
end repeat
Script 1 Result needs parsing...
"Joe BloggsMolly MousePeter Pan"
to this...
{"Joe Bloggs", "Molly Mouse", "Peter Pan"}
so I can use it in Script 2 menuItems below.
Script 2 MUST use names from Script 1 Result, like this
-- Make Pop-Up Menu in Numbers spreadsheet which is already open.
set menuItems to {"Joe Bloggs", "Molly Mouse", "Peter Pan"}
tell application "Numbers"
activate
tell the first table of the active sheet of document 1
tell cell "A3"
set value to item 1 of menuItems
set the format to pop up menu
end tell
end tell
end tell
tell application "System Events"
tell application process "Numbers"
set frontmost to true
tell window 1
click radio button "Cell" of radio group 1
repeat with i from 2 to (count menuItems)
click button 1 of group 1 of scroll area -1
keystroke (item i of menuItems)
end repeat
end tell
end tell
end tell
The first script is going through the items of a list list to create a string, while the second script is just wanting the list. Since the Contacts application will give you a list of names, the first script isn't needed at all - the menuItems of the second script can just be set with:
tell application "Contacts" to set menuItems to the name of people in group "testGroup"

run a sub-script for every item of a list on AppleScript

I have a list, and I want which every element of this list (order number)
to run a search, grab text from chrome and navigate through a website using this order number.
I have already both script (A and B)
I thought I could just add my long script (B) to the first one, inside
repeat with theItem in theResult
Script B
end repeat
but this is not working, I get the error
Expected “end” but found “property"
Script B e.g :
tell application "Google Chrome"
tell front window's active tab to set infoGrab to execute javascript "document.getElementsByClassName('even')[2].innerHTML;"
end tell
set theText to Unicode text
set theSource to infoGrab
property leftEdge72 : "<a href=\"/"
property rightEdge72 : "\">"
set saveTID to text item delimiters
set text item delimiters to leftEdge72
set classValue to text item 2 of theSource
set text item delimiters to rightEdge72
set uniqueIDKey to text item 1 of classValue
set text item delimiters to saveTID
uniqueIDKey
and more.
I then tried to save script B in an independent script and run from script A like this
repeat with theItem in theResult
set the clipboard to theItem
set myScript to load script file ((path to desktop folder as text) & "SEARCH.scpt")
tell myScript
end tell
delay 30
end repeat
but this don't work neither, the script B since to ignore all repeat and delay and just run everything instantaneity with no action on goole chrome
Question : How to I get to do other action for each element of a list including text-delimiter and more.
PS : sorry if my post is confusing.
I think your script is flawed, since "Script B" does NOT use "theItem" variable from the repeat loop. So, "Script B" will return the same result for every item.
Your objective is not clear. Perhaps if you provided more details, with real-world example data of both the source HTML and the expected results we could provide better help.
IAC, why are you calling a script instead of using a handler?
Here's your scripts refactored to use a handler, as an example.
repeat with theItem in theResult
## You don't seem to use "theItem" in the Script B ##
# if so, then the getID() handler will return the same results for all items in this loop
set myID to my getID()
end repeat
on getID() -- was Script B
-- put in same script file, or in script library
-- does this need to have a parameter?
tell application "Google Chrome"
tell front window's active tab to set infoGrab to execute javascript "document.getElementsByClassName('even')[2].innerHTML;"
end tell
set theText to Unicode text
set theSource to infoGrab
set leftEdge72 to "<a href=\"/"
set rightEdge72 to "\">"
set saveTID to text item delimiters
set text item delimiters to leftEdge72
set classValue to text item 2 of theSource
set text item delimiters to rightEdge72
set uniqueIDKey to text item 1 of classValue
set text item delimiters to saveTID
return uniqueIDKey
end getID
run script alias ((path to desktop folder as text) & "SEARCH.scpt")

Automator workflow - extracting mail message source urls inserting "=" + linebreak inside URLs

Referencing this solution, I am trying to create an Automator script that will extract a URL from behind an HTML email button. However, getting the mail message source instead of content returns rich text that breaks the url links by "=" and linebreaks, leaving me with results like "http://www.=" and "http://www.w3=" instead of the full urls. How can I get the url from a mail message source HTML email?
The current Automator Workflow looks like this:
Get Selected Mail Messages
Run Applescript:
on run {input, parameters}
set mailContentList to {}
tell application "Mail"
repeat with selectedMail in input
set end of mailContentList to source of selectedMail
end repeat
end tell
return mailContentList
end run
Extract URLs from Text
Do more stuff with those URLs
I have tried inserting an Applescript step between 2 and 3 to process the text to remove the "=" & linebreak, but my code does not seem to change anything. Here is what I have tried:
on run {input, parameters}
set AppleScript's text item delimiters to {"=" & return & linefeed, "=" & return, "=" & linefeed, "=" & character id 8232}
set theTextItems to every text item of input
set AppleScript's text item delimiters to ""
set theText to theTextItems as string
set AppleScript's text item delimiters to ""
return theText
end run
Any ideas on how to either process the buggy rtf that inserts extra = and linebreaks, or get the input in a different format that returns the button hrefs as full URLs?
I ended up solving this by using a Run JavaScript block instead of the Applescript block:
function run(input, parameters) {
var text = input[0].replace(/=(?:\r\n|\r|\n)/g, '')
return text;
}
which fixes the buggy behavior.

How do I make this AppleScript Folder Watch ignore hidden items?

I have pretty much spent my entire Sunday trying to find an answer to this and I am now admitting defeat and asking for your help. I've read several examples on here and some other AppleScript sites but non of them seem to work when applied to this script. I do however put this down to me.
I'm trying to use the script below to watch a folder then send me an email when a new item has been added and it works just fine. The trouble I am having is getting it to ignore system [or hidden] folders.
I have removed my efforts to do this as they all kept breaking the whole thing, so the version below works but currently it will include hidden items.
property theName : "Name"
property theAddress : "name#company.com"
on adding folder items to this_folder after receiving added_items
set added_Items_List to {}
repeat with oneItem in added_items
set end of added_Items_List to name of (info for oneItem)
end repeat
set {TID, text item delimiters} to {text item delimiters, ", "}
set added_Items_List to added_Items_List as text
set text item delimiters to TID
set dateString to (current date) as string
set theBody to "There are new files in the FTP folder " & name of (info for this_folder) & ":" & return & return & added_Items_List
tell application "Mail"
set newMessage to make new outgoing message with properties {visible:true, subject:"New submissions on FTP: " & dateString, content:theBody}
tell newMessage
make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
end tell
activate
send newMessage
end tell
end adding folder items to
Any help or direction would be much appreciated and hopefully others will benefit from a working solution.
For anyone who come across this post with a similar issue, you may find the answer to your problem on one of these posts.
Applescript System Events Returns .DS_Store - How can I ignore
How to I tell this Applescript to skip some files?
AppleScript : How to get files in folder without hidden files?
Hidden files start with a dot, just filter those items
on adding folder items to this_folder after receiving added_items
set added_Items_List to {}
repeat with oneItem in added_items
set fileName to name of (info for oneItem)
if fileName does not start with "." then
set end of added_Items_List to fileName
end if
end repeat
if (count added_Items_List) = 0 then return
....
But if the Finder on your machine does not show the hidden files by default you don't need the additional lines.

Weird behavior from the results of "Get Link URLs from Webpages" action

I'm building an Automator workflow that parses a Dropbox "gallery" page containing Quicktime video files, and automatically builds "direct-download" links for each of the files. In the end, I want all the links to be generated into the body of a Mail.app message.
Basically, everything works as expected, except for the part where the new links are sent to a new Mail.app message. My problem is that the links are sent to the body of the message without newlines, so they all get concatenated into a single line, like this:
https://dl.dropbox.com/u/149705/stackexchange/20121209/stackexchange_automator_prob2.png
(Mail.app seems to be wrapping the concatenated string on the question-marks)
The oddest thing about this is that if I use a "Copy to Clipboard" action at the end of the workflow (instead of my send-to-Mail Applescript), I get totally different results when I paste the same clipboard contents into TextEdit vs. BBEdit.
The links seem to paste into TextEdit properly. However, the same clipboard, pasted into a plaintext BBEdit document, yields only the first link:
https://dl.dropbox.com/u/149705/stackexchange/20121209/stackexchange_automator_prob5.jpg
What could be causing these 3 entirely different behaviors from the exact same results of the "Get Link URLs" action?
The reason you get that result is it the links start out as a List of strings but are being sent to mail.app as a single string.
You do not need to do anything fancy to fix this just put a return after the link.
I would just use this a single 'Run applescript' Action to do the whole job.
The script gathers only the file DL links. Adds a return on the end and send them to Mail.app.
No other formatting required
on run {input, parameters}
set db_tag to "dl-web.dropbox.com/get"
set myLinks to {}
tell application "Safari"
set thelinkCount to do JavaScript "document.links.length " in document 1
repeat with i from 1 to thelinkCount
set this_link to (do JavaScript "document.links[" & i & "].href" in document 1) as string
if this_link contains db_tag then
--add the link with a carriage return on the end.
copy this_link & return to end of myLinks
end if
end repeat
end tell
tell application "Mail"
activate
make new outgoing message with properties {visible:true, content:"" & myLinks}
end tell
end run
Get rid of all of the other actions and update myPage with your gallery URL.
on run
set myPage to "https://www.dropbox.com/sh/aaaaaaaaaaaaaaa/aaaaaaaaaa"
set myLinks to do shell script "curl " & quoted form of myPage & " | grep -Eo 'https://www.dropbox.com/sh/[^/]*/[^/\"]*/[^\"]*' | sed s'/https:\\/\\/www.dropbox.com\\(.*\\)/https:\\/\\/dl.dropbox.com\\1?dl=1/g'"
tell application "Mail"
activate
make new outgoing message with properties {visible:true, content:"" & myLinks}
end tell
end run
Each application is different, it understand or don't understand the output's format.
Regarding your AppleScript action:
input is an AppleScript list, the coercion from a empty string "" do a single line, because the delimiters is set to "" by default.
To get the desired result, set the delimiters to return or linefeed, like this.
on run {input}
set {oTID, text item delimiters} to {text item delimiters, linefeed}
set tContent to input as text
set text item delimiters to oTID
tell application "Mail"
activate
make new outgoing message with properties {visible:true, content:tContent}
end tell
return input
end run

Resources