Basically I want the script to display a dialog with an answer. In my case the text will be a URL, After I click “ok” I need the script to search a specific text document for a exact line that matches the dialog answer, If that lines does exist within the specified text document, do nothing, If that line DOES NOT exist add it to the document but only one url per line.
So, where do all these URLs come from? If you just need to scan a file (or files) of URLs to synchronize another document, there is no need for a dialog box. How about a few more details?
Urls come from anywhere, Think of the urls as my favorite site urls, and the text document as bookmark file
I want to enter my favorite url link dialog box, when I click ok, I need the script to check if the text returned of dialog is contained in the bookmark file. If the text is currently added to bookmarks file, do nothing but if the text does not exists in the bookmark file, add the text returned to the bookmark file. Each URL should be contained in 1 line
I enter “aol.com” in dialog answer box
it checks bookmark file if the line “aol.com” exists, if it does not exist script will add it to file and will look this
" google.com yahoo.com aol.com
"
Is this something like what you want.
Tries to works with url in the Clipboard
set file_path to "Users/USERNAME/Desktop/url.txt"
set The_Url to the clipboard as string
if The_Url contains ".co.uk" or The_Url contains ".com" or The_Url contains "http://" then
set The_Url_answer to The_Url
else
set The_Url_answer to ""
end if
display dialog "Enter Url" default answer The_Url_answer buttons {"Cancel ", "Check"} default button 2
copy the result as list to {text_returned, button_pressed}
if button_pressed is "Check" then
set The_Check to ""
try
set The_Check to do shell script "cat " & file_path & "| grep -iw " & quoted form of text_returned
end try
if The_Check is "" then
do shell script "echo " & return & text_returned & " >> " & file_path
display dialog text_returned & return & return & " Has been added to list" buttons {"OK"} default button 1
else
display dialog text_returned & return & return & " Exist's in List Already" buttons {"OK"} default button 1
end if
end if
Mark’s version, but using AppleScript’s text reading/writing funcitons.
set filePath to "HD:Users:USERNAME:Desktop:url.txt"
set urlData to read file filePath
set theUrl to the clipboard as string
if theUrl contains ".co.uk" or theUrl contains ".com" or theUrl contains "http://" then
set UrlAnswer to theUrl
else
set UrlAnswer to ""
end if
display dialog "Enter Url" default answer UrlAnswer buttons {"Cancel ", "Check"} default button 2
set {textReturned, buttonPressed} to result as list
if buttonPressed is "Check" then
if urlData does not contain UrlAnswer then
set fileRef to (open for access file filePath with write permission)
write textReturned & return to fileRef starting at eof
close access fileRef
display dialog UrlAnswer & " has been added to the list"
else
display dialog UrlAnswer & " already exists in the list"
end if
end if
Thank You, both scripts work as I wanted. I also like how its take the text in the clipboard so I don’t have to paste in the dialog box just only copy is needed.
Hi James, I noticed that in your version, if the file is empty the script would error.
So I added an error handler.
Also for some reason the textReturned needed to be made into a string, before the check would match correctly existing urls. odd??
set filePath to "Macintosh HD:Users:USERNAME:Desktop:url.txt"
try
set urlData to read file filePath
on error
set urlData to ""
end try
set theUrl to the clipboard as string
if theUrl contains ".co.uk" or theUrl contains ".com" or theUrl contains "http://" then
set UrlAnswer to theUrl
else
set UrlAnswer to ""
end if
display dialog "Enter Url" default answer UrlAnswer buttons {"Cancel ", "Check"} default button 2
set {textReturned, buttonPressed} to result as list
set textReturned to textReturned as string
if buttonPressed is "Check" then
if urlData does not contain UrlAnswer then
set fileRef to (open for access file filePath with write permission)
write textReturned & return to fileRef starting at eof
close access fileRef
display dialog UrlAnswer & " has been added to the list"
else
display dialog UrlAnswer & " already exists in the list"
end if
end if
Another way to avoid an error when attempting to read a potentially empty file is to check the end of file (eof) marker first.
The reason for apparent matches not being recognised is down to a fundamental difference between plain and Unicode text. The text returned from a dialog is Unicode “ and, without a conversion to string, is written to file as such.
When read back as text (the read command’s default), it consists of two bytes per character. If the Unicode character was derived from a regular ASCII character, the first byte of each pair would normally be a null character (ASCII character 0), and would therefore not be visible. If you want to preserve the Unicode text, don’t alter the dialog output “ but read the file as Unicode text instead.
When writing, however, remember that concatenating the return character (plain text) with the dialog output (Unicode text) can result in an implicit coercion to string. So the return character should really be coerced to Unicode text first. (Life can sure get complicated, right?)
Here’s a short demo of these points (albeit without the luxury of your other bells and whistles):
property file_path : (path to desktop as Unicode text) & "Bookmarks.txt" (* modify path/filename as required *)
set new_URL to text returned of (display dialog "Enter or paste a new URL:" default answer return)
if (count new_URL) > 1 then
set file_ref to open for access file file_path with write permission
if (get eof file_ref) is 0 then -- new file
write new_URL to file_ref
else -- file has been written to
if new_URL is not in (read file_ref as Unicode text) then write (return as Unicode text) & new_URL to file_ref
end if
close access file_ref
end if