Interact WITH a running Applescript script/program without leaving the

The general question from my specific problem is how to interact WITH a running Applescript without leaving the program you a currently in. Its kind of the opposite question of how to USE Applescript to interact with other programs.

I need to go through a lot of web pages and input information from them into a spreadsheet. I have somewhat automated the process with an Applescript that takes an webpage as input and then opens its relevant links one by one, waiting for a click in a dialog box before proceeding to the next.

If you want to run the script, use http://www.resultat.dk/tennis/challenger-singler/champaign/resultater/ as input to the dialog box

--Let the user input the the main webpage
display dialog "Linkzor" default answer ""
set tunering to text returned of result

--get the javascript of the web page
tell application "Safari"
	
	tell window 1
		open location tunering
	end tell
	delay 5
	tell front document to set ¬
		startText to {name, do JavaScript "window.document.documentElement.outerHTML"}
end tell
set listen to {}
set teksten to startText as string

--Gets all the relevant link variables of the web page
repeat with i from 1 to 500
	
	set forekomst to text (nthOffset(teksten, "g_2_", i) + 4) thru (nthOffset(teksten, "g_2_", i) + 11) of teksten
	if nthOffset(teksten, "g_2_", i) = 0 then exit repeat
	
	set punkt to "http://www.resultat.dk/kamp/" & forekomst & "/#kampreferat"
	set listen to listen & punkt
end repeat

set lengde to count of listen

--opens the links one by one
repeat with x from 1 to lengde
	
	tell application "Safari"
		tell window 1
			set URL of document 1 to item x of listen
		end tell
	end tell
	
	--set question to display dialog "luk vinduet?" buttons {"Ja"}
	--set answer to button returned of question
	set question to display dialog "forsæt?" buttons {"Ja", "nej"} default button 1
	set answer to button returned of question
	if answer is "nej" then exit repeat
	
end repeat




on nthOffset(myText, subString, n)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to subString
	-- There will be one more text item than there are instances of the substring in the string, so:
	if (count myText's text items) > n then
		-- There are at least n instances of the substring in the text.
		-- The first character of the nth instance comes immediately after the last
		-- character of the nth text item.
		set o to (count text from text item 1 to text item n of myText) + 1
	else
		-- There isn't an nth instance of the substring in this text.
		set o to 0
	end if
	set AppleScript's text item delimiters to astid
	return o
end nthOffset

Now I would like to automate it a bit further so I didn´t have to leave the spreadsheet to click the dialog box when going to the next webpage. Is there a way to force the Applescript proceed without me actually clicking the dialog box? I have tried to save the script as an application (“hent2”) and made another script to sent an “enter” keystroke to it while it is running (and then activate the new script through some kind of hotkey):

tell application "System Events"
    tell application "hent2" to activate
    key code 36
end tell

It does´t do anything.

Any suggestions on how to proceed? Either with the “sent keystroke to Applescript” or a more elegant solution?

You may encapsulate the script in an automator service and give it a shortcut.
You may also use a script launcher like FastScripts which allow us to give shortcuts to scripts.
It’s my preferred choice. FastScripts is free if you don’t need to trigger more than ten scripts.
If you need more it costs $9.95.

Look at : https://red-sweater.com/fastscripts/

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) jeudi 10 décembre 2015 16:07:48

Thank you for taking the time with my question.

I think I might not have described the problem well enough.

The problem isn´t how to start a script, the problem is how to interact with a running script without bringing it to the foreground.

The script creates a list of urls and open the first in Safari and as it is now it will display a dialog box and wait for me to click a button the load the next url in the list. I need a method that allows me to either click the “ja” button or alter the script altogether so I can “tell” the script to continue the loop through some other means than clicking the “ja” button. All without leaving the foreground spreadsheet application.

Well thats a first on an Applescript related questions;)

Stacksoverflow was faster than Macscripter

For future reference, the solution to the problem can be found here:

http://stackoverflow.com/questions/34198251/interact-with-a-running-applescript-script-program-without-leaving-the-current-p

Some users who helps others here as well, me included, are SO users too. They are simple Q&A sites not designed for discussions or follow up questions. Therefore when an question comes by on SO as well I don’t answer them on MS, and I speak for a few other MS members here as well. When the question is asked on an Q&A site as well, it implies that the TS doesn’t look for an answer that can evolve to be the perfect answer through multiple posts. Remember that MS and SO are two different things and they do their tasks at best.