trying to return string with unknown middle characters

hey there im here to bother you all again

i run my own website and i am trying to retrieve certain paragraphs of the page using safari. the starting string is always the same and so is the end string but being links the last 6 characters of the paragraph is always different…

for example:


the id is always changing and i am hoping to be able to set the id to a string variable in applescript

Here are some ideas for you to check out:

set test to "<p>Blah, Blah, <a href=\"viewimage.php?id=902384\">Link</a>, More Blah</p>"
return text 16 thru 49 of test

set test to "<p>Here are some links: <a href=\"viewimage.php?id=000123\">Link A</a>, <a href=\"viewimage.php?id=000456\">Link B</a>, <a href=\"viewimage.php?id=000789\">Link C</a></p>"

set ASTID to AppleScript's text item delimiters
set idList to {}
set AppleScript's text item delimiters to {"href=\"viewimage.php?id="}
try
	get text items 2 thru -1 of test
	
	repeat with thisItem in result
		try
			set idList's end to text 1 thru 6 of thisItem
		end try
	end repeat
end try
set AppleScript's text item delimiters to ASTID

return idList

thanks mate… now just one more question for you… how do i search the page for these links so they can be returned… there are normally 30 links on each page.

your examples worked great btw :wink:

Try something like this:

display dialog "URL:" default answer ""

try
	do shell script "/usr/bin/curl -fsS " & quoted form of (text returned of result)
	set webPage to result
on error errMsg number errNum
	display dialog "Error " & errNum & return & return & errMsg buttons {"Cancel"} default button 1
end try

set ASTID to AppleScript's text item delimiters
set idList to {}
set AppleScript's text item delimiters to {"href=\"viewimage.php?id="}
try
	get text items 2 thru -1 of webPage
	
	repeat with thisItem in result
		try
			set idList's end to text 1 thru 6 of thisItem
		end try
	end repeat
end try
set AppleScript's text item delimiters to ASTID

-- Do something with `idList`.

tried to just return the idList and all i get is {}… any ideas? :stuck_out_tongue: yeah i know… im a noob !

lol all good… i did one of the old “insert a character by accident and overlook it…”

thanks for your help bruce !!!

lol jsut realised that all of the ids are not of the same length… so i suppose i have to use the start and end character of the string to identify the id i need… is there a way i can do this… all the links end in \

I’m jumping in at the end here, but if the numbers are of differing length then something like this?


set ids to {"<a href=\"viewimage.php?id=902384\">", "<a href=\"viewimage.php?id=9027384\">"}
set idNums to {}
repeat with I in ids
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "viewimage.php?id="
	set N to text item 2 of I
	set AppleScript's text item delimiters to tid
	set end of idNums to text 1 thru -3 of N
end repeat

What if we use grep?

display dialog "URL:" default answer ""

try
	do shell script "/usr/bin/curl -fsS " & quoted form of (text returned of result) & " | /usr/bin/grep -o '\"viewimage.php?id=[0-9]*' | /usr/bin/cut -d = -f 2"
	set idList to paragraphs of result
on error errMsg number errNum
	display dialog "Error " & errNum & return & return & errMsg buttons {"Cancel"} default button 1
end try

-- Do something with `idList`.

okay thanks again guys… its all good until i have some of the ids with letters in it as well

for example…

please come to my rescue my david hasslehoff of applescript… :smiley:

What if we use grep, but we tell it to find both number and letters? (Edit: and remove a quote.)

display dialog "URL:" default answer ""

try
	do shell script "/usr/bin/curl -fsS " & quoted form of (text returned of result) & " | /usr/bin/grep -o 'viewimage.php?id=[[:alnum:]]*' | /usr/bin/cut -d = -f 2"
	set idList to paragraphs of result
on error errMsg number errNum
	display dialog "Error " & errNum & return & return & errMsg buttons {"Cancel"} default button 1
end try

-- Do something with `idList`.