AppleScript cURL and parse URL - applescript

I am working on a Automator workflow, I am passing list of URLs to the "Run Applescript" and I need to fetch the contents of on each page, concatenate and pass it to a BBedit (or any other text editor).
on run {input, parameters}
tell application "BBEdit"
activate
set astid to AppleScript's text item delimiters
set startHere to "<tbody>"
set stopHere to "</tbody>"
repeat with anItem in input
set blurb0 to (do shell script "curl " & anItem)
set AppleScript's text item delimiters to startHere
set blurb1 to text item 2 of blurb0
set AppleScript's text item delimiters to stopHere
set blurb2 to text item 1 of blurb1
set AppleScript's text item delimiters to astid
return blurb2
beep
end repeat
end tell
end run
The current code only properly gets only the contents from first URL. Can anybody fix this?

This subroutine may be what you need (if you're using Safari)...
on getSource(this_URL)
tell application "Safari"
activate
set the URL of the current tab of document 1 to this_URL
set the |source| to the source of the front document
end tell
tell application "TextEdit"
activate
set the text of the front document to the source
end tell
quit application "Safari"
end getSource
Call it using:
repeat with anItem in input
getSource(input)
end repeat
I hope this helps!

Because you are inside the repeat with anItem in input loop it does all your work for the first item and the return exits the loop and in fact the whole Automator action. I think you have never heard the beep from your script ;-)
On the other side I'm wondering why you want BBEdit to do the cUrl and sort work for you. All called handlers don't belong to BBEdit...
I think your handler should look like this:
on run {input, parameters}
-- define a few parameters
set astid to AppleScript's text item delimiters
set startHere to "<tbody>"
set stopHere to "</tbody>"
-- define a list to store all found content
set allFoundContent to {}
repeat with anItem in input
set blurb0 to (do shell script "curl " & anItem)
set AppleScript's text item delimiters to startHere
set blurb1 to text item 2 of blurb0
set AppleScript's text item delimiters to stopHere
-- put the found content at the end of the list
set end of allFoundContent to text item 1 of blurb1
set AppleScript's text item delimiters to astid
end repeat
-- from here you have three possibilities:
-- 1. return the list to next Automator action (uncomment the next line):
-- return allFoundContent
-- 2. concatenate the list with a delimiter you like (here return & "------" & return)
-- and give it to your preferred text editor from this point (uncomment the next lines):
-- set AppleScript's text item delimiters to return & "------" & return
-- tell application "TextEdit"
-- make new document with properties {text: allFoundContent as text}
-- end tell
-- set AppleScript's text item delimiters to astid
-- 3. concatenate the list with a delimiter you like (here return & "------" & return)
-- and give it to the next workflow step, maybe a BBEdit action waiting for a string? (uncomment the next lines):
-- set AppleScript's text item delimiters to return & "------" & return
-- set returnString to allFoundContent as text
-- set AppleScript's text item delimiters to astid
-- return returnString
-- Next decision (for choice 1 or 2):
-- What do you want to give to next Automator action?
-- you can pass your input (the given URLs) (uncomment next line):
-- return input
-- or your result list (uncomment next line):
-- return allFoundContent
end run
Greetings, Michael / Hamburg

Related

Applescript Replace all <br> tags with empty lines?

This code gets two bits of two and copies them to the clipboard.
One bit of text is html. Before stripping out all the html I want to replace the tags with empty lines.
I can't get the replaced correctly, but the rest of the code works.
Any idea what I am doing wrong. I also tried using return instead of linefeed.
tell application "GarageSale 7"
repeat with theListing in (get selected ebay listings)
set des to get the description of theListing
set comment to get private comment of theListing
end repeat
end tell
set theText to des
to searchReplace(thisText, "<br>", linefeed)
set AppleScript's text item delimiters to searchTerm
set thisText to thisText's text items
set AppleScript's text item delimiters to replacement
set thisText to "" & thisText
set AppleScript's text item delimiters to {""}
return thisText
end searchReplace
on removeMarkupFromText(theText)
set tagDetected to false
set theCleanText to ""
repeat with a from 1 to length of theText
set theCurrentCharacter to character a of theText
if theCurrentCharacter is "<" then
set tagDetected to true
else if theCurrentCharacter is ">" then
set tagDetected to false
else if tagDetected is false then
set theCleanText to theCleanText & theCurrentCharacter as string
end if
end repeat
return theCleanText
end removeMarkupFromText
get the clipboard
set the clipboard to removeMarkupFromText(theText) & comment
You've defined a handler called searchReplace(), but you never actually use it. That's why your script isn't replacing the <br> tags.
Firstly, you want to define the handler properly. It should take arguments that are represented by variables; currently, your last two arguments are specific values:
to searchReplace(thisText, "<br>", linefeed)
Here's a suggested edit:
to searchReplace(thisText, searchTerm, replacement)
set my text item delimiters to searchTerm
set thisText to thisText's text items
set my text item delimiters to replacement
set thisText to thisText as text
set my text item delimiters to {""}
return thisText
end searchReplace
Then you can call it from your script like so:
tell application "GarageSale 7"
repeat with theListing in (get selected ebay listings)
set des to get the description of theListing
set comment to get private comment of theListing
end repeat
end tell
set theText to searchReplace(des, "<br>", linefeed)
set the clipboard to removeMarkupFromText(theText) & comment

AppleScript for change file names inside folders

I need to write an AppleScript that can change my files names inside folder.Below is my scenario.
Folder1
|
Folder2
|
imageA.png,imageB.png
Now i need to write an AppleScript that can change name of imageA.png to A.png and imageB.png to B.png.
What should be my AppleScript for this? this is not my exact scenario but i am giving an example above so i can implement more from that.
I have tried below script and it is working well but i am getting error when filename is not exist, means it is not ignoring it.
tell application "Finder"
set the name of file "iOs_142:About1.png" to "About1.png"
set the name of file "iOs_142:FAQs.png" to "FAQs1.png"
end tell
If you really insist on doing this with AppleScript, this should give a template to work forward with:
(* Taken from http://applescript.bratis-lover.net/library/string/#replaceString *)
on replaceString(theText, oldString, newString)
local ASTID, theText, oldString, newString, lst
set ASTID to AppleScript's text item delimiters
try
considering case
set AppleScript's text item delimiters to oldString
set lst to every text item of theText
set AppleScript's text item delimiters to newString
set theText to lst as string
end considering
set AppleScript's text item delimiters to ASTID
return theText
on error eMsg number eNum
set AppleScript's text item delimiters to ASTID
error "Can't replaceString: " & eMsg number eNum
end try
end replaceString
tell application "Finder"
set fs to files of folder POSIX file "/your/folder" as list
end tell
repeat with f in fs
set n to replaceString(name of f, "to be replaced", "")
tell application "System Events" to set name of f to n
end repeat

How to rename a folder and some of its content from a text returned variable

What i want is to do a find and replace command with applescript. I know I can use namechanger and other programs but that takes too many steps. I'm all about efficiency.
tell application "Finder"
set selected_items to selection
set jobNum to text returned of (display dialog "Job Number:" default answer "")
set name of document folder selection to jobNum (*if it did work it would rename the entire folder which isn't what I want. My goal is to replace all "12345" with the value of jobNum*)
end tell
sample folder structure (i can't submit images yet because I need 10pts)
main folder: 12345_Sample Job Folder
- 12345_D.ai
- 12345_P.ai
- Proofs
- Working Files
- 12345.ai
Try this script:
set jobFolder to choose folder with prompt "Choose the Job Folder:"
tell application "Finder"
set rootName to jobFolder's name
set searchString to my FindSearchString(rootName)
set replaceString to text returned of (display dialog ("This script will search for files that contain " & searchString & ". Please enter the replacement text and click OK.") default answer searchString)
set everything to every file in (jobFolder's entire contents)
repeat with onething in everything
if ((onething's name) as text) contains searchString then
set onething's name to my replace_chars(((onething's name) as text), searchString, replaceString)
end if
end repeat
set jobFolder's name to my replace_chars(((jobFolder's name) as text), searchString, replaceString)
end tell
---------------------------------
on replace_chars(this_text, search_string, replacement_string)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to astid
return this_text
end replace_chars
---------------------------------
to FindSearchString(txt)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "_"
set sst to txt's text item 1
set AppleScript's text item delimiters to astid
return sst
end FindSearchString
This will only affect filenames within the initial chosen folder, not subfolder names. At the very end, it changes the name of the chosen folder.
The renaming handler is based on the code at the bottom of this page

Avoid AppleScript to make line breaks

I'm using this AppleScript to get the content of a selection of eMails:
using terms from application "Mail"
on run {input, parameters}
set mailContents to {}
repeat with aMessage in input
set end of mailContents to content of aMessage
end repeat
return mailContents
end run
end using terms from
The result looks like this:
{"

Here's some text followed by a longer URL, which is cut by a line break!
http://www.XYZ.net/diario/actualidad/economia/20140714/-ciudad-bar
ata-para-el-turismo_353_34541.html
"}
I want to use all links in those specific mails for fort her processing but it's not possible to use the divided URLs as seen in the example before.
So how to tell the AppleScript to keep the URL together?
Try this. I started with your mailContents example. You end up with a list of the links in the httpLinks variable. This works assuming every link starts with "http" and ends with "html". Good luck.
set mailContents to {"

Here's some text followed by a longer URL, which is cut by a line break!
http://www.XYZ.net/diario/actualidad/economia/20140714/-ciudad-bar
ata-para-el-turismo_353_34541.html
"}
set httpLinks to {}
repeat with i from 1 to count of mailContents
set thisContent to item i of mailContents
-- remove all return characters (mac, unix, and windows characters)
set AppleScript's text item delimiters to {character id 10, character id 13, character id 13 & character id 10}
set textItems to text items of thisContent
set AppleScript's text item delimiters to ""
set newText to textItems as text
-- find links
set AppleScript's text item delimiters to "http"
set textItems to text items of newText
if (count of textItems) is greater than 1 then
set AppleScript's text item delimiters to "html"
repeat with j from 2 to count of textItems
set linkItems to text items of (item j of textItems)
set thisLink to "http" & item 1 of linkItems & "html"
set end of httpLinks to thisLink
end repeat
end if
set AppleScript's text item delimiters to ""
end repeat
return httpLinks
This does not break lines, hope you can use it somehow:
tell application "Mail"
set mailSelection to selection
set mailContents to {}
repeat with mail in mailSelection
set end of mailContents to content of mail
end repeat
get mailContents
end tell
Thank you very much! I had to customize your script a bit because not every URL ends with html. Furthermore I used the source of the messages instead of content, because the deleted line breaks had the result, that it was very difficult to separate the URLs from other text. There was no space between them. This is my actual code:
`using terms from application "Mail"
on run {input, parameters}
set mailContents to {}
repeat with aMessage in input
set end of mailContents to source of aMessage
end repeat
set httpLinks to {}
repeat with i from 1 to count of mailContents
set thisContent to item i of mailContents
-- remove all return characters (mac, unix, and windows characters)
set AppleScript's text item delimiters to {"=09", "=" & character id 10, "=" & character id 13, "3D", character id 92}
set textItems to text items of thisContent
set AppleScript's text item delimiters to ""
set newText to textItems as rich text
-- find links
set AppleScript's text item delimiters to "http"
set textItems to text items of newText
if (count of textItems) is greater than 1 then
set AppleScript's text item delimiters to "target"
repeat with j from 2 to count of textItems
set linkItems to text items of (item j of textItems)
set thisLink to "http" & item 1 of linkItems & "target"
set end of httpLinks to thisLink
end repeat
end if
set AppleScript's text item delimiters to ""
end repeat
set list1 to httpLinks
set list2 to {}
repeat with x from 1 to count of items of list1
set n to item x of list1
if n is not in list2 then set end of list2 to n
end repeat
return list2
end run
end using terms from`

Send email from clipboard without opening mail.app -- with conditions

I asked the question Send email from clipboard without opening mail.app and got the code
set a to "myemail#mail.com"
tell application "Mail"
tell (make new outgoing message)
set subject to (the clipboard)
set content to "content"
make new to recipient at end of to recipients with properties {address:a}
send
end tell
end tell
now I wonder, how could I have a script that do the same thing, but modifies it like this: if the Subject is the first 10 words, and iff the clipboard har more than 10 words, then the clipboard is cut off. For example like this "hello there baby this is a long message sent with... [see notes]" and then the enitre message (i.e. "hello there baby this is a long message sent with my new email, see you.") is in the content of the email.
Replace the set subject ... and set content ... lines in your script with the following:
if (count of words of (the clipboard)) is greater than 10 then
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set subject to ((words 1 through 10 of (the clipboard)) & "... [see notes]") as text
set AppleScript's text item delimiters to oldDelims
set content to (the clipboard)
else
set subject to (the clipboard)
set content to "content"
end if
Links to references:
count of gives the number of elements in a list
words of splits a text string into a list, with each element representing a word
AppleScript's text item delimiters can be manipulated to help split and join lists with different delimiters
the through keyword can be used to get a subrange of items from a list
#adayzdone has a good point - sometimes using words of to split a string to a list then reassembly with text item delimiters can mess up the input data. You could do this instead:
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set cblist to text items of (the clipboard)
set AppleScript's text item delimiters to oldDelims
if (count of cblist) is greater than 10 then
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set subject to ((items 1 through 10 of cblist) & "... [see notes]") as text
set AppleScript's text item delimiters to oldDelims
set content to (the clipboard)
else
set subject to (the clipboard)
set content to "content"
end if

Resources