views:

179

answers:

3

I'm starting to poke around with Applescript and am looking at writing a few scripts for managing windows. A common task they will all need is to get the current screen size.

I've created a screen_size subroutine that seems to work, and I want to be able to share that with all my scripts. However, I can't figure out a way to put that in a separate file that I can load in my other scripts. I tried creating a separate screen_size.scpt file and use load script "screen_size.scpt", but I get an error about "can't make "screen_size.scpt" into a type file".

There has to be a way to do this, but I haven't been able to find anything online about how to do it.

EDIT:

The POSIX stuff suggested isn't working for me. I'm able to create the file object, but it refuses to convert to an alias, saying it can't find the file (looks like the POSIX file stays relative instead of expanding fully).

I found a suggestion online to use Finder, and have gotten the following working to get an alias:

tell application "Finder"
  set _myPath to container of (path to me) as text
end tell

set _loadPath to (_myPath & "screen_size.scpt")
set _loadAlias to alias _loadPath

However, the next line fails with a syntax error, claiming that _loadAlias isn't a variable:

property _ScreenSize : load script _loadAlias

Every variation of this I've tried (doing the alias in the load call, etc) fails, always claiming the variable doesn't exist, even though I know it's being set and working as I can display it. What's going on? Why is it claiming a variable doesn't exist when it obviously does?

A: 

Yes there is a way to do this. Load the file into a property and access it that way

property _ScreenSize : load script (alias "pathtoscript")

_ScreenSize's doStuff()

and for relative paths try this:

set p to "./screen_size.scpt" 
set a to POSIX file p 

so perhaps this will work:

set p to "./screen_size.scpt" 
set a to POSIX file p 

property _ScreenSize : load script (alias a)

_ScreenSize's doStuff()
Ulve
can "pathtoscript" be relative (so, just the name of the script since it's in the same directory)?
Herms
I tried it using just the file name and get a syntax error: `File alias screen_size.scpt of <<script>> wasn't found.` The code I have is `property _ScreenSize : load script (alias "screen_size.scpt")`
Herms
Don't know much about applescript but I think the best way is to use POSIX paths if you want relative paths.
Ulve
You need the full path load script (alias "hd:lib:screen_size.scpt")
Ulve
or (path to me):screen_size.scptI have no mac at work so I cannot check that it works.
Ulve
Trying to keep it generic (might throw these up on github so other people could use them), so I don't want to use absolute paths at all. I'll try the path to me thing.
Herms
try this:set p to "./screen_size.scpt" set a to POSIX file p then a should resolve to something like hd:your folder:screen_size.scptthen a should re
Ulve
no good. I can't use the resulting file in the load (claims the variable isn't defined), and I can't convert it to an alias either.
Herms
A: 

Yes you can. You need the full path to the script however.

I believe you can still use "path to me" to get the path to the app executing the current script, and you can then modify that path to point to your sub-folder containing the scripts.

I used this technique to get around AppleScripts (former) 32k text size limits years ago for some really large/complex IRC scripting.

I think I still have all those old scripts in my G4, which is under the desk in my office at work. Sadly it's behind a Enet switch and I can't VNC into it otherwise I'd have tons of sample code to post.

ExitToShell
how would I use "path to me" to do it? "path to me" gives me the full path to the script that's running *including the script filename*. I need to remove the file name from that and add the name of the script I want to include, but I can't figure out how to do that.
Herms
A: 

AppleScript is doing some really weird things when saving and I haven't figured out what's going on, but I ended up getting something to work.

Here's what I have:

on load_script(_scriptName)
  tell application "Finder"
    set _myPath to container of (path to me) as text
  end tell

  set _loadPath to (_myPath & _scriptName)
  load script (alias _loadPath)
end load_script

set _ScreenSize to load_script("screen_size.scpt")

set _bounds to _ScreenSize's screen_size()
-- ...

The other answers were saying to set _ScreenSize as a property, but that would cause a syntax error which prevented me from ever saving the file. When I did it just using set it worked.

I wasn't ever able to get the POSIX path stuff suggested to work, but poking Finder for the path worked fine.

Herms