All,
I am new to AppleScript, and I am looking for some help in completing a script I wrote to look up a person’s name in our web directory at work. I learned how to do most of the code by searching, but I am stuck at one point in the implementation. I currently have it set up that if you are looking for ‘James Davis’, you enter ‘Davis, James’ with the AppleScript delimiter set to “,”. My code works just fine as long as one enters the “,”.
The problem occurs when one just enters the last name and no “,”. I assume there is something simple I am missing, but any help that you can all give me would be greatly appreciated. If this has been answered before I apologize in advance. (I removed the website in the code since it is a secure site for work)
property theLookup : ""
property theLookup1 : ""
property theLookup2 : ""
--------------------------------------------
-- Enter a name for the search
--------------------------------------------
tell application "Microsoft Outlook"
display dialog "Enter the name of the person you want to find: (Ex: Davis, James)" default answer theLookup with icon 1 with title "Lookup Name"
set theLookup to text returned of result
try
set oldDelims to AppleScript's text item delimiters -- save their current state
set AppleScript's text item delimiters to {", "}
set theLookup1 to text item 2 of theLookup
set theLookup2 to text item 1 of theLookup
on error
set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong
end try
end tell
--------------------------------------------
-- Load the web page
--------------------------------------------
tell application "Safari" to open location "https://www.website.com"
if page_loaded(20) then
say "done"
else
say "failed"
end if
on page_loaded(timeout_value) -- in seconds
delay 2
repeat with i from 1 to timeout_value
tell application "Safari"
if name of current tab of window 1 is not "Loading" then exit repeat
end tell
delay 1
end repeat
if i is timeout_value then return false
tell application "Safari"
repeat until (do JavaScript "document.readyState" in document 1) is "complete"
delay 0.5
end repeat
end tell
return true
end page_loaded
--------------------------------------------
-- Do the search
--------------------------------------------
tell application "Safari"
do JavaScript "window.document.forms[0].elements[0].value = \"" & theLookup1 & "\"" in document 1
do JavaScript "window.document.forms[0].elements[2].value = \"" & theLookup2 & "\"" in document 1
do JavaScript "window.document.forms[0].elements[7].value = 'JSC'" in document 1
do JavaScript "window.document.forms[0].elements[8].click()" in document 1
end tell
set theLookup to ""
set theLookup1 to ""
set theLookup2 to ""
Hi. Welcome to MacScripter.
It can be quite a business catching everything the user might enter. :rolleyes: You could allow for either “<surname(s)>, <christian name(s)>” or just “<surname(s)>” like this:
display dialog "Enter the name of the person you want to find: (Ex: Davis, James)" default answer "" with icon 1 with title "Lookup Name"
set theLookup to text returned of result
try
if (theLookup contains ",") then
set oldDelims to AppleScript's text item delimiters -- save their current state
set AppleScript's text item delimiters to {","}
set theLookup1 to text from word 1 to word -1 of text item 2 of theLookup
set theLookup2 to text from word 1 to word -1 of text item 1 of theLookup
set AppleScript's text item delimiters to oldDelims -- restore them afterwards
else
set theLookup1 to missing value
set theLookup2 to text from word 1 to word -1 of theLookup
end if
on error errMsg
display dialog errMsg buttons {"Exit"} default button 1
error number -128
end try
The presence or not of a comma in the input decides how to interpret it. The ‘text from word 1 to word -1’ parts of the references strip leading or trailing spaces or punctuation, while allowing double-barrelled names. The value of theLookup1 will be ‘missing value’ when no christian name’s specified, so you’ll need to make the relevant ‘do JavaScript’ line dependent on it not being that.
As far as I can see, you don’t need the ‘theLookup’ variables to be properties, so you should delete the property declarations.
Your page_loaded() handler definition would be better placed at the beginning or end of the script so that all the “run handler” code is together in one block.
Thank you very much Nigel! There is one change I had to make, but everything works now! Thank you very much.
I do have a question about your suggestion of moving the code for opening the webpage to the beginning. Is moving that more proper for code purposes? I ask because the goal I am trying to reach with this, is a user will click Ctrl-F while in outlook, enter the name of the person they are looking for, and the script will open the webpage and execute the search. I wrote the script since we were having problems with the new outlook and our directory search, and since the web directory was our “workaround” I was trying to make it seamless for a user to perform the web search.
Here is the finished code if anyone out there is trying to do something like this. The one change I made to Nigel’s code is I used a wildcard instead of ‘missing value’ for defining the variable. This I believe stopped me from having to do anything in the javascript portion of the code to account for that. Please let me know if there is something wrong with that approach, since I am new to this.
Again, thank you very much for your help!
--------------------------------------------
-- Enter a name for the search
--------------------------------------------
tell application "Microsoft Outlook"
display dialog "Enter the name of the person you want to find: (Ex: Davis, James)" default answer "" with icon 1 with title "Lookup Name"
set theLookup to text returned of result
try
if (theLookup contains ",") then
set oldDelims to AppleScript's text item delimiters -- save their current state
set AppleScript's text item delimiters to {","}
set theLookup1 to text from word 1 to word -1 of text item 2 of theLookup
set theLookup2 to text from word 1 to word -1 of text item 1 of theLookup
set AppleScript's text item delimiters to oldDelims -- restore them afterwards
else
set theLookup1 to "*"
set theLookup2 to text from word 1 to word -1 of theLookup
end if
on error errMsg
display dialog errMsg buttons {"Exit"} default button 1
error number -128
end try
end tell
--------------------------------------------
-- Load the web page
--------------------------------------------
tell application "Safari" to open location "https://www.website.com"
if page_loaded(20) then
say "done"
else
say "failed"
end if
on page_loaded(timeout_value) -- in seconds
delay 2
repeat with i from 1 to timeout_value
tell application "Safari"
if name of current tab of window 1 is not "Loading" then exit repeat
end tell
delay 1
end repeat
if i is timeout_value then return false
tell application "Safari"
repeat until (do JavaScript "document.readyState" in document 1) is "complete"
delay 0.5
end repeat
end tell
return true
end page_loaded
--------------------------------------------
-- Do the search
--------------------------------------------
tell application "Safari"
do JavaScript "window.document.forms[0].elements[0].value = \"" & theLookup1 & "\";" in document 1
do JavaScript "window.document.forms[0].elements[2].value = \"" & theLookup2 & "\";" in document 1
do JavaScript "window.document.forms[0].elements[7].value = 'JSC';" in document 1
do JavaScript "window.document.forms[0].elements[8].click();" in document 1
end tell
Yes. That bit of code (from ‘page_loaded(timeout_value)’ to ‘end page_loaded’) is a “handler” ” a block of code which isn’t part of the flow you have around it but is “called” from the ‘if’ statement above.
The rest of the code, which doesn’t appear to be in a handler, is technically in an “implicit run handler”. (An explicit run handler would begin with ‘on run’ and end with ‘end run’. There’s no functional difference between implicit and explicit run handlers.) It happens to be possible to put an ordinary handler anywhere you like inside an implicit run handler, but it’s not considered good form to interrupt the code for one handler with the code for another. In any other case (as far I can recall), it’s impossible.
Putting your page_loaded handler either at the beginning or the end of the script won’t make it work any better, but will keep its code visually separate from the run handler code.
on page_loaded(timeout_value) -- in seconds
delay 2
repeat with i from 1 to timeout_value
tell application "Safari"
if name of current tab of window 1 is not "Loading" then exit repeat
end tell
delay 1
end repeat
if i is timeout_value then return false
tell application "Safari"
repeat until (do JavaScript "document.readyState" in document 1) is "complete"
delay 0.5
end repeat
end tell
return true
end page_loaded
--------------------------------------------
-- Enter a name for the search
--------------------------------------------
tell application "Microsoft Outlook"
display dialog "Enter the name of the person you want to find: (Ex: Davis, James)" default answer "" with icon 1 with title "Lookup Name"
set theLookup to text returned of result
try
if (theLookup contains ",") then
set oldDelims to AppleScript's text item delimiters -- save their current state
set AppleScript's text item delimiters to {","}
set theLookup1 to text from word 1 to word -1 of text item 2 of theLookup
set theLookup2 to text from word 1 to word -1 of text item 1 of theLookup
set AppleScript's text item delimiters to oldDelims -- restore them afterwards
else
set theLookup1 to "*"
set theLookup2 to text from word 1 to word -1 of theLookup
end if
on error errMsg
display dialog errMsg buttons {"Exit"} default button 1
error number -128
end try
end tell
--------------------------------------------
-- Load the web page
--------------------------------------------
tell application "Safari" to open location "https://www.website.com"
if page_loaded(20) then
say "done"
else
say "failed"
end if
--------------------------------------------
-- Do the search
--------------------------------------------
tell application "Safari"
do JavaScript "window.document.forms[0].elements[0].value = \"" & theLookup1 & "\";" in document 1
do JavaScript "window.document.forms[0].elements[2].value = \"" & theLookup2 & "\";" in document 1
do JavaScript "window.document.forms[0].elements[7].value = 'JSC';" in document 1
do JavaScript "window.document.forms[0].elements[8].click();" in document 1
end tell
I understand now, your definitions, and pulling out that portion of the code really helped cement it in my mind. Thank you very much again, I learned a lot from this and I look forward to learning more and helping others. Cheers!