I don’t understand your question but this is the shortest you can get
open location "http://www.marketwatch.com/investing/stock/GGG"
open location "http://www.marketwatch.com/investing/stock/CHRW"
open location "http://www.marketwatch.com/investing/stock/BPOP"
open location "http://www.marketwatch.com/investing/stock/DCT"
All URLs will be opened with the standard browser. and open location isn’t an safari command so you don’t need the safari’s tell block.
Ultimately, I would like to use a display dialog command to input stock symbols (e.g. “GGG”, “CHRW”, “BPOP”, “DCT”) and combine that data with the URL, or others, in order to open the pages.
In other words, I want to write this so I can input the stock symbols and stem URL.
-- If marketwatch has a webservice/api you shouldn't use the following line.
-- Instead you should fetch the latest stock names from marketwatch.
-- Even better if marketwatch supports xmlrpc or soap services.
set stockNames to {"GGG", "CHRW", "BPOP", "DCT"}
set baseURL to "http:www.marketwatch.com"
set selectedStock to choose from list stockNames with title "Select Stock" empty selection allowed no multiple selections allowed no
open location (baseURL & "/investing/stock/" & selectedStock as string)
As an alternate, if you wanted to open more than one of the pages at a time, you could either use a repeat loop to make up and use the full URL to each item or (as it turns out) use the “open” shell script, which accepts Unix-like multi-URLs. eg.
-- If marketwatch has a webservice/api you shouldn't use the following line.
-- Instead you should fetch the latest stock names from marketwatch.
-- Even better if marketwatch supports xmlrpc or soap services.
set stockNames to {"GGG", "CHRW", "BPOP", "DCT"}
set baseURL to "http://www.marketwatch.com"
set selectedStock to (choose from list stockNames with title "Select Stock" with multiple selections allowed without empty selection allowed)
if (selectedStock is false) then error number -128
if ((count selectedStock) > 1) then
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set selectedStock to "{" & selectedStock & "}"
set AppleScript's text item delimiters to astid
end if
do shell script "open " & baseURL & "/investing/stock/" & selectedStock
Actually, you can shorten it further (more lines, but almost 100 fewer characters).
set a to "http://www.marketwatch.com/investing/stock/"
open location a & "GGG"
open location a & "CHRW"
open location a & "BPOP"
open location a & "DCT"
set a to "http://www.marketwatch.com/investing/stock/"
set b to text returned of (display dialog "Stock Symbol:" default answer "" with title "Stock Checker")
open location a & b
The number of characters in ApplesScript source code is completely irrelevant, apart from legibility considerations. From the point of view of what the code actually does, DJ’s script is “shorter” than yours. It simply takes the provided URLs and feeds them to the ‘open location’ command. Yours has to build the URLs from the bits first, which means it does more work and uses more memory to accomodate both the complete URLs and the bits.
Your script’s perfectly OK, but DJ’s is more efficient and (to my eyes) more immediately legible.