Running multiple scripts in an application

I have tried searching for this, but am none the wiser.

Up until now I have only used a single script in my ASS apps, but I come to the point where it’s getting a bit ridiculous having everything in one file. The trouble is I jave no experience of working with multiple scripts - how would you call the second script from the main script?

what I really want to do is place a script that runs when the app is launched in it’s own script file. I have tried this by making a “on will finish launching” handler connected to the second script, but that seems to disconnect the “on will finish launching” handler in my main script , and also disconnect other “Application owner” handlers.

As you can see I am confused. If anyone could shed any light on this for me I would be most grateful.

thanks,

arum

I am a bit confused because you seem to be interchanging terms. You said…

A handler is IN a script, not connected to it. An object is connected to the handler in a script via an event.

You can’t. Not easily, at least. Events in your interface can call handlers through events, but to paraphrase apple poorly “applescript provides no easy way for scripts to communicate between each other” (sorry, couldn’t find the exact quote quickly :wink: ). Typically, you group similar tasks and handlers in one script, and create multiple scripts for multiple uses. Having a “launch” script, one that does all of your app’s launch-time things is a good idea. It keeps the code that’s only used once in one, out-of-the way place. In reference to having one script, Apple says…

As apple says, if you want to use code for two things, you need to have them all in one script. You can’t break up the task and then just call the second script when you need the data or a handler from it. You can only have an object (i.e. button, window, file owner) attached to a single script. You cannot have an object, a window for example, call code from two scripts on launch without doing some magic. You’re better off just putting the two things you want to do into one script and then call them as code in the “on will finish launching” handler or as subroutines.

So, create your script , i.e. “launcher.applescript” with an “on will finish launching” handler. Put all the code in it that you want to execute on launch. Then set the file owner’s “on will finish launching” handler to point at the code. Like I said you can’t have the file’s owner call code from both “launcher.applescript” AND “someotherscript.applescript”. You’ve got to pick one, or use another event like “on awake from nib” to access the other script. This is probably the reason for your other events becoming ‘disconnected’ from their other events. When you switch to a second script, it either requests the handler from the new script and returns an error because it’s not there, or it adds the handler automatically to the new script with no code in it, which will obviously return nothing. Or, you could cut&paste your code into the second script :).

Some of the sample AS projects that come with PB have practical examples and explanations of using multiple scripts. Nothing complex, just a simple breakdown of the separate tasks into separate scripts.

Good Luck…
j

You can move some routines to a secondary script and then in the first script, at launch, load the second script into memory and then use that script object to do something:

script_a.applescript:

property script_b : missing value

on will finish launching
	set script_b to load script (path to me as string) & "Contents:Resources:Scripts:script.b.scpt"
end will finish launching

on do_something()
	display dialog (script_b's add_numbers(1, 2))
end do_something

script_b.applescript:

on add_numbers(a, b)
	return a + b
end add_numbers

This works best if there aren’t persistent properties that need to be maintained in script_b. If there are, then you need to add a little more but it’s still feasible.

Hope this helps.

Jon

yeah, sorry jobu - I meant event not handler.

i’ll try what you guys suggest.

thanks.

well, I’m still very much confused :frowning:

I have been playing around with what you both suggested but I’m not having any luck so far.

Just to clariy, this is what I want to do:

I have a seperate script which has been tested and works when run in Srcipt Editor.
This script generates a code unique to the machine, and checks an inputted code.
if the inputted code is not correct it needs to quit my app. this script has quite a few seperate handlers in it

the problem is when I try to put this in my main script for my ASS app it stops working .
So I thought If I could have it in it’s own seperate script file it should work. But I can’t seem to make a new script that runs when called from the “Launched” event of the “file’s owner” in interface builder (do you follow me?).
I have managed to connect this event to a handler in the new script, whilst apparently leaving the rest of my event handlers (will finish launching, awake from nib etc) in the original script.
I say apparently because the original script and all of it’s handlers work as they should, but the “launched” handler in the second script is not working. it currently contains a “display dialog” (event??) for testing, but the dialog is not showing.

Any ideas why this might not be working?? or maybe there is a way to seperate the “Code number” script and it’s handlers whilst still keeping it in the same file as the original script - I don’t know…

once again, thanks for helping, if you have any more that would be great!!

arum

I think we’re about as far as we can go without you posting the problem code. I suspect this is some sort of registration code and that you may not wish to reveal it and, if so, you’re probably on your own.

Jon

never mind all that, I found a way to do it.
all I needed was this insoide my main script:


on someevent 
tell script_a to run
end someEvent


script script_a
-- code script
end

I was aware of this kind of “object” existing, but didn’t know the syntax and was having trouble finding out how to do it. I finally found it in the Applescript language guide (it’s not easy to find something when you don’t know what its’ called!!!).

I do have somehting else though (surprise surprise :wink: )

how can I write a file without a “choose file name” window opening? eg I want my app to make a file somwhere that stores a date for expiration of a demo period, and for obvious reason I don’t want the user to choose where this file goes!!

also any ideas as to a safe (hidden) place to put the file/ can I make an invisible file??

cheers.

arum

Maybe you can store your date in an invisible, run only script file within your app’s package. I have never attempted to do this so I don’t know if it’s even possible, reasonable or practical.

Note: You might need to use the shell (osacompile, osascript) to handle some or all of it.

– Rob

that’s no good because then the user could reset the demo period by simply re-installing the app, and anyway I don’t even know how to make a file invisible…
thanks anyway!!

maybe someone knows of a better way to do this any way?

Using invisible files is really a token effort as far as security goes. You can usually see the invisible files using basic finder views, and you can find plenty of apps out there that make them visible, if not. There are ways to get some level of security, but applescript has limitations that simply do not afford a truly secure environment. As jonn8 said in another thread, you won’t find many scripters that have good, solid ways of doing this who will reveal their secrets. I think that the first factor when determining the security necessary for your app is ‘Who is the end user?’ Are you marketting this to other applescripters? If so, most of us could hack it apart pretty quickly given the right motivation ;). If you’re selling it to ‘regular folks’, I would say most of them would not even know how to view the package contents of an app… and if they did they wouldn’t know how to begin figuring out what the “jumble” of characters and symbols meant (assuming a deployment style package and a reasonable attempt at encrypting your data).

I think we can safely assume that most ‘regular users’ are not browsing this forum for ways to crack $5 shareware apps. Since ‘diminishing returns’ are a problem anyways, the few enthuiastic app crakers looking through here for crumbs of info are a drop in the bucket. And…since most of us here could beat the system anyways, sharing knowledge among peers (at least some) would be a good discussion and may help us all find a “better way”. That said, arumdevil, you’re on a good path with the “hidden” file concept…a data file that is not just sitting out in the open. I’m actually using two files, both identical, but in two places. The second file is a bit ‘hidden’ and remote, to try to check if the user has trashed the app and re-installed (of course if they find the second file, re-installing is still possible). I hide the first in the application package itself (:Contents:Resources:THEFILE). This is good because no matter where you put the app, the file is always in the same place. I “encrypt” the path to the file in my script and scatter the encrypion keys and values for all of my encryption throughout the script, so it is not totally obvious what things are when reading the script in a text editor like bbedit. I also write a copy of the file to a remote place, like the library directory or somewhere in the system directory (assuming you want to do some authorization/permissions magic). Anyways, use a totally arbitrary name for the two files, but something that seems appropriate and sounds “official” so as to not draw attention to itself. I recommend changing the file’s creation and modification dates so the file can not be searched for in sherlock according to it’s dates. Unfortunately, there aren’t a whole lot of places to hide things, and creating a whole new directory on a computer with an observant user is a pretty obvious change, so that’s a bad route in my opinion. As I said earlier, having a good encryption method is also a good idea and probably warrants much more attention than I’m giving it here.

As I mentioned to you in another post, I am working on a ‘prepackaged’ system I will make available as soon as it’s somewhat complete. I code for fun, so making my system available to everyone won’t ruin my security needs. I figure, if anything, people can find ways to improve on or modify the system I build to provide the security they are looking for. Realize though that you could end up doubling your file size just adding a simple, easily defeated security measure. Without solid knowledge of shell use and probably some unix tricks, AScripters should be realistic about what level of security they truly have.

When I finish my code, you’ll be welcome to it.
j

EDIT…
An afterthought…I change the modification dates and creation dates of the files to hide them from the search, but I also actually use the dates to do some of the time-stamp checking. Even if a user finds the file, they probably will not think that the actual date of the file is important. Since the creation date comes from the date it was saved on YOUR computer, not when it was installed on the user’s, only a savvy user would see or know how to overcome this obstacle.

jobu, thanks for that post.

high security for this case is not what I need. In any case I have successfully created and integrated into my app a code system that will provide me with a satisfactory level of protection. however, I would like to offer a full featured, time-limited demo period of my App rather then a crippled “cut down” demo feature set.
That is why I was asking about invisible files. Of course I realize that an invisible file is not invisible to someone who really wants to fid it, but in this case I don’t think it will be much of an issue. - my market for this app are solely Computer DAW (Digital Audio Workstation) users of a specific DAW. Therefore most of the people who will be interested in my App are professional musicians/ Studio owners who make their living trying to sell something that is becoming increasingly hard to sell!! (ie. music). Most of these guys are very strict about paying people for their creative effort and so are unlikely to try to rip me off - which is a nice bonus!!!

As for your suggestion of making two files to check on - that sounds like a good idea to me! (I would still like to know how to make a file invisible though - as I haven’t been able to do this…if you could tell me that would be good).

Regarding your “Prepackaged” system which you mention here and in another topic - that sounds brilliant. I initially thought you and jonn8 were the same poster, so I thought you had given up on that as a result of somehthing he (I thought you) said in reply to that thread.

thanks so much for all your input guys,

arum

Here are some sites that seem to be a good starting point for making files/folders invisible. Most will need some external resources, like terminal use or scripting additions, except the following.

I haven’t tried it (I will soon) but I particularly like the idea of the boot into os9, create a “.file”, then reboot into osX. Check out what I mean HERE near the end of the first post.

<< EDIT >>
OK, so I tried the above and it works VERY WELL! Reboot into os9 and go into your project’s build directory. Create the file you want to use as a data file, and save it as whatever you want to call it, preceded by a period, i.e. “.SecretData”. The Dot before the name is KEY! Unix (thus osX) treats all files starting with a period as invisible. Luckily, os9 does not. Then reboot into osX and the file’s gone! You’ll need to search for the file, including a “Visibile: All” criteria to have it come up in a search. Works very well, especially if you use a name that is either REALLY common or REALLY unique. Perhaps you could also copy the file to some ‘remote’ location, as I discussed in a post above, as your second data file. If you are able to copy a “.file” using applescript to another location, it would have to retain it’s invisibility because it’s name it what dictates it’s visibility. This would be an added measure of security for us AS guys :smiley:
<< EOEDIT >>

More Links…
[url=http://forums.osxfaq.com/viewtopic.php?t=3605]http://forums.osxfaq.com/viewtopic.php?t=3605[/url] (Same as above)
[url=http://forums.osxfaq.com/viewtopic.php?t=4302]http://forums.osxfaq.com/viewtopic.php?t=4302[/url]
[url=http://www.latenightsw.com/freeware/FileVisibility/]http://www.latenightsw.com/freeware/FileVisibility/[/url] (Scripting addition)
[url=http://www.macosxhints.com/article.php?story=20011129130906725]http://www.macosxhints.com/article.php?story=20011129130906725[/url]

Good Luck…
j

To create an invisible file, all you need to do is create it with a “.” preceding the name, no need to boot into 9, then back into X:

set the_file to (((path to "cusr") as string) & ".unique_name.txt")
my write_to_file(the_file, "some text")

try
	get the_file as alias
	set file_was_written to true
on error
	set file_was_written to false
end try

try
	--uncomment the next line to actually delete the file, after checking to see if it is invisible, of course
	--do shell script "rm " & (quoted form of POSIX path of the_file)
end try
return file_was_written

on write_to_file(the_file, the_string)
	set the_file to the_file as file specification
	try
		open for access the_file with write permission
		write (the_string as text) to the_file
		close access the_file
	on error
		try
			close access the_file
		end try
	end try
end write_to_file

Jon

arum…in your case, i.e. simply writing data to a file, you should not have a problem doing it Jon’s way. How you use the file will determine whether his way is reasonable or not. If you are using it to store lots of data, especially if significant data comes with the package, and then you add to it or modify it later, you may find it easier to create the document manually in a text editor, and then dump it in under os9.

After testing a bit more, I have confirmed my suspicion that using terminal/shell commands may be the only way to access the file’s attributes, which is MY main objective. Finder is so much easier, but I’ll do whatever it takes to get it done right. Using finder to access the file type and creator codes, as well as the creation and mod dates of an invisible file I haven’t found to be possible yet. It seems that making them invisible means that AS cannot access them via the finder, either :cry:. Any known means of getting around this? The SetFile command in terminal is part of developer tools, right? so won’t most mac’s NOT have it available? Any other comparible tools to get/set codes and dates?

j

I don’t understand this at all. Why are we even talking about OS 9? You can write any type of data (strings, lists, records, whatever) using the handler I posted above and there is no need for OS 9 no matter what the data.

Here is a handler which should do everything you need:

set the_file to (((path to desktop) as string) & ".unique_filename.txt")
set the_data to {item_1:"item_1", item_2:1, item_3:{1, 2, 3}}
set with_appending to false
set creator_code to "R*ch"
set file_type to "TEXT"
set touch_date to "200212311231" -- YYYYMMDDHHMM
set the_result to my write_to_file(the_file, the_data, with_appending, creator_code, file_type, touch_date)
set the_info to info for file the_file
set read_data to (read file the_file as (class of the_data))
do shell script "rm " & (quoted form of POSIX path of the_file) --kill the file
return {the_result, the_info, read_data}

on write_to_file(the_file, the_data, with_appending, creator_code, file_type, touch_date)
	set the_file to the_file as file specification
	try
		open for access the_file with write permission
		if with_appending = false then set eof of the_file to 0
		write the_data to the_file starting at eof as (class of the_data)
		close access the_file
		try
			tell application "Finder"
				if creator_code is not "" then set creator type of (the_file) to creator_code
				if file_type is not "" then set file type of (the_file) to file_type
			end tell
		on error
			tell application "System Events"
				if creator_code is not "" then set creator type of (the_file) to creator_code
				if file_type is not "" then set file type of (the_file) to file_type
			end tell
		end try
		do shell script "touch -t " & touch_date & " " & (quoted form of POSIX path of the_file)
		return true
	on error the_error
		try
			close access the_file
		end try
		return the_error
	end try
end write_to_file

Jon

Slow down there Jon :?, we’re all here for fun, aren’t we? As a perl and html guy, I am still much more comfortable with my text editor than I am with ASS. I never implied that you HAD to write the file externally and then use os9, I just mentioned that if it was appropriate, as it would be in my application of this concept, that it may be better to do it this way. Consider having to write a couple hundred/thousand characters to this file. Yes, AS can do that, but having the overhead of all of this extra data in your script just to initialize the file on the first launch of the program seems rediculous. There is no way to remove that data from the script, and every subsequent time you open the program, those extra characters still must be evaluated and processed, even if you never use them again. Why not just write the file in a text editor, and insert it into the installation package? That way you only have to reference the file and it’s data, without the bloat of having the data stored in the app for eternity when you don’t need it there. That’s the whole reason for having the external file…so it’s NOT in the script. There are also oxs utilities that can allow the creation of invisible files without terminal/shell (which some of us do not fully understand or wish to), and of course you could write ANOTHER script to write the file for you.

:rolleyes:
j

Slow down? Have fun? Are you suggesting that you are picking up hostility in my posts? If so, that is completely something you are adding on your own and not something I intended. I have only added concrete, constructive posts in this thread.

Even if you need to use an external editor to create your file (which I don’t understand since we’re in the AS Studio forum and AS Studio is an incredibly powerful editor for many types of documents), why does OS 9 keep coming up? If you need an external editor, try an OS X editor though I urge you to learn more about AS Studio which has terrific search and replace, color coded syntax, split editor screens, etc.

Also, you seem to have some misconceptions about AppleScript. Having hundreds, thousands, even tens of thousands of extra characters in your scripts tucked away in handlers that are only used once (or not even at all) will not impact your script (take a look at any of the example files provided by Apple with tons of extraneous copyright & warranty information that has no bearing on the actual scripts). It may be unruly for you to wade through a script with extra content. If this is the case, just add a .strings file to your AS Studio app and read that in at first launch and use the handler I provided to write the file which allows you to set all the attributes you’ve requested (visibility, creator code, file type, creation date, modification date, variable data). It should work on all installations and doesn’t require you to learn any complicated shell commands (it relies only on a single shell command–the ‘touch’ command; the ‘rm’ command is not part of the handler–and it is fully formed, fairly self-explanatory, and doesn’t need any more understanding on your part to make it work).

Jon

that is fantastic jonn8 ! i’m nearly there. Just a few questions:

how do I change the creation date of a file? (if you answered this before in this thread then ignore, I am about to go back and check!).

(path to “cusr”) get’s me to the user’s home, (path to desktop) get’s me to the user’s desktop, but how do I get to the main Library folder (at root level of the system HD)?
I need to write my invisible file here so the demo expires for the whole machine, not just one user.

as to joub’s concerns about writing lots of data to the file, In this case that is not an issue. In fact, I don’t need to write any data to the fila at all. I just need to create it and change the creation date.

I also have a problem with my app remembering a file path from one launch to the next. If I can’t get it to work I’ll see if you guys can help figure it out.

thanks a lot guys, :slight_smile:

arum

In the handler I provided before, when you “touch” the file with the date provided, it changes both the creation and modification dates to that date. Here’s the code to get the main library folder path:

set path_to_Library to (path to "dlib") as string

Jon

Firrstly, I have just uploaded the first public release of my Application to my website, and I want to thank you guys for your help, especially jonn8. you helped me sort out a lot of issues and without your help it wouldn’t have got where it is as quickly. So thanks.

Secondly, this app needs to query another for certain states, but as the application it interacts with isn’t actually scriptable I have to do this via GUI scripting. All I need to do is get states and names of a few menu items - the trouble is the application in question is very lazy with it’s menu’s and only updates them when they are visible - which makes them useless for my cause.

for example I need to know when the app (Digital performer, which I will refer to as DP from here on) is recording. I can find this out by getting the state of the “Save” menu item because this becomes disabled when DP is recording, but DP only updates this when I actually click the menu with the mouse.
So… is there a way to make the menu update before I get it’s properties??

cheers,

arum