I’ve just finished an app that opens a URL in Safari. It has the option to show bookmarks and let the user choose one. But when I try to change the bookmarks, the app stores them until it quits. This is my script:
I try to change and store my bookmarks in the variable myBookmarks
-- Open URL.applescript
-- Open URL
-- Created by ief2 on 21/10/09.
-- Copyright 2009 __MyCompanyName__. All rights reserved.
global myBookmarks
property myBookmarks : {"google.com", "facebook.com", "yahoo.com"}
on awake from nib theObject
(*Add your script here.*)
end awake from nib
on clicked theObject
-- als OKURL
if name of theObject is "OKURL" then
set myURL to contents of text field 1 of window "GiveURL"
OpenURL(myURL)
quit
end if
-- als Voeg Toe
if name of theObject is "Voeg Toe" then
set myDial to (display dialog "Welke URL wil je toevoegen?" default answer "Voorbeeld: 'Google.com'" buttons {"Annuleer", "OK"} default button 2)
if button returned of myDial is "OK" then ¬
set (contents of table view 1 of scroll view 1 of window "FavorietenEdit") to ((contents of table view 1 of scroll view 1 of window "FavorietenEdit") & ((text returned of myDial)))
end if
-- als Verwijder
if name of theObject is "Verwijder" then
set mySelRows to selected data rows of table view 1 of scroll view 1 of window "FavorietenEdit"
set myDial to (display dialog "Ben je zeker dat je de geslecteerde sites wil verwijderen?" buttons {"Neen", "Ja"} default button 2)
if button returned of myDial is "Ja" then
repeat with i in mySelRows
delete i
end repeat
end if
end if
-- als Annuleer
if name of theObject is "Annuleer" then
show window "GiveURL"
hide window "FavorietenEdit"
end if
-- als OKFav
if name of theObject is "OKFav" then
set myTableContents to contents of table view 1 of scroll view 1 of window "FavorietenEdit"
set myBookmarks to myTableContents
show window "giveurl"
hide window "FavorietenEdit"
end if
-- Als open favorieten
if name of theObject is "Favorieten" then
show window "Open Fav"
hide window "giveurl"
set contents of table view 1 of scroll view 1 of window "Open Fav" to myBookmarks
end if
-- Als AnnuleerOpenFav
if name of theObject is "AnnuleerOpenFav" then
show window "giveurl"
hide window "Open Fav"
end if
-- Als OKOpenFav
if name of theObject is "OKOpenFav" then
try
set myURL to contents of data cell 1 of selected data row of table view 1 of scroll view 1 of window "Open Fav"
set contents of text field 1 of window "giveurl" to ("http://" & myURL) as string
end try
hide window "Open Fav"
show window "giveUrl"
end if
end clicked
on should quit after last window closed theObject
return false
end should quit after last window closed
on choose menu item theObject
-- als Edit Favorieten
show window "FavorietenEdit"
hide window "GiveURL"
set contents of table view 1 of scroll view 1 of window "FavorietenEdit" to myBookmarks
end choose menu item
on will close theObject
if name of theObject is "GiveURL" then quit
end will close
(* ===== HANDLERS ===== *)
on OpenURL(URLToOpen)
tell application "System Events"
get every process
if result contains "Safari" then
tell application "Safari"
set myDoc to (make new document with properties {URL:URLToOpen})
activate myDoc
end tell
else
tell application "Safari"
activate
close every document
set myDoc to (make new document with properties {URL:URLToOpen})
activate myDoc
end tell
end if
end tell
end OpenURL