Hi all,
Please can sombody tell me why a script would run fine from script editor, but will not run from the script menu?
Any help would be greatly appreciated.
Thanks in advance.
Model: eMac
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)
Ok here is my script, It should simply get extract the image urls from the safari window with focus, and create an html document with the images, then redirect to that document.
--Get current users username
set username to do shell script "/usr/bin/whoami"
--Path to temp web page
set temp_file to "Macintosh HD:Users:" & username & ":Desktop:temp.html"
--Get url
tell application "Safari"
--Get the pages URL
set pageUrl to URL of document of window 1
--Break it down to the name of the server
set AppleScript's text item delimiters to "/"
set partsOfPageUrl to pageUrl's text items
set serverUrl to item 3 of partsOfPageUrl
--e.g(if it was "http://www.google.co.uk/search?blah&blah=blah" will now be "[url=http://www.google.co.uk]www.google.co.uk[/url]")
end tell
--Javascript to fetch the image urls for me
set someJavascript to "var imageUrl = \"\";
var maxImages = document.images.length;
for(i = 0;i<maxImages;i++) {
imageUrl = imageUrl + \"*\" + document.images[i].src;
}
return imageUrl;"
--Tell Safari to process the javascript on the webpage, and return the results to imageUrls
tell application "Safari" to set imageUrls to (do JavaScript someJavascript in document of window 1)
--Split the string into the seperate urls and store them in a list
set AppleScript's text item delimiters to "*"
set theImages to imageUrls's text items
--Set text item delimiters back
set AppleScript's text item delimiters to ""
--Get rid of any items that my not be a url
repeat with iUrl from 2 to (count theImages)
if (item iUrl of theImages) does not end with ".jpg" or ".gif" or ".png" then
delete item iUrl of theImages
else if (item iUrl of theImages) is equal to "" then
delete item iUrl of theImages
else if (item iUrl of theImages) does not start with "www" or "http" then
delete item iUrl of theImages
end if
end repeat
--Create the html for the page
set startHtml to "<html><head><title>Extracted images</title></head><body>"
set startHtml to startHtml & "<center><h2><font color=\"blue\">Images from " & serverUrl & "</font></h2>"
set startHtml to startHtml & "<h3><a href=\"" & pageUrl & "\">" & pageUrl & "</a>"
set endHtml to "</center></body></html>"
set middleHtml to ""
repeat with image from 1 to (count theImages)
set middleHtml to middleHtml & "<br /><a href=\"" & (item image of theImages) & "\"><img src=\"" & (item image of theImages) & "\"></img></a>"
end repeat
set theHtml to startHtml & middleHtml & endHtml
--write the html to a temp file
set htmlFile to open for access temp_file with write permission
write theHtml to htmlFile
close access htmlFile
--finally tell safari to display the page
tell application "Safari" to open temp_file
It runs fine, when i run it from within script editor, but when i put it in the script menu folder, and choose it form the menu it seems to do nothing at all.
I am new to applescript so it probably wil be somthing simple lol
The problem is the row of deletes. There’s no such command in the AppleScript language itself, only in certain applications. For some reason, it’s being tolerated without erroring in Script Editor, but not in Script Menu. Also, the or statements in that section are badly formed. If you want to delete items from a list (or, more properly, to get another list without those items in it), you could do something like this:
--Get rid of any items that my not be a url
repeat with iUrl from 2 to (count theImages)
if not (((item iUrl of theImages) ends with ".jpg") or ((item iUrl of theImages) ends with ".gif") or ((item iUrl of theImages) ends with ".png")) then
set item iUrl of theImages to missing value
else if (item iUrl of theImages) is equal to "" then
set item iUrl of theImages to missing value
else if not (((item iUrl of theImages) starts with "www") or ((item iUrl of theImages) starts with "http")) then
set item iUrl of theImages to missing value
end if
end repeat
set theImages to theImages's every Unicode text
That could be condensed to:
--Get rid of any items that my not be a url
repeat with iUrl from 2 to (count theImages)
set thisUrl to item iUrl of theImages
if (thisUrl is "") or not ((thisUrl ends with ".jpg") or (thisUrl ends with ".gif") or (thisUrl ends with ".png") or (thisUrl starts with "www") or (thisUrl starts with "http")) then
set item iUrl of theImages to missing value
end if
end repeat
set theImages to theImages's every Unicode text
I’d recommend writing the code that writes the HTML to file like this:
--write the html to a temp file
set htmlFile to open for access file temp_file with write permission
-- Use a 'try' block to ensure that any errors won't stop the script before the access is closed again.
try
set eof htmlFile to 0 -- Delete any existing file contents.
write theHtml to htmlFile
end try
close access htmlFile
--finally tell safari to display the page
tell application "Safari" to open file temp_file
Otherwise, that’s a pretty cool script for someone who’s new to AppleScript.