4

In the first scenario, I have a script containing all of my handlers and snippets. The filename for this script is “Jimz_Handlerz.scpt” and it is located here… /Users/Smokestack/Library/Script Libraries

Calling the handler from a new AppleScript file requires me to declare my script library (line 1) and then I can call the handler at anytime I want with the second line. Like this:

use myHandlerz : script "Jimz_Handlerz.scpt"

myHandlerz's get_fileExtensions()

In this next scenario, I am using the “load script” command to load the exact same file as the first example but this file is located on the desktop

property jimzHandlerz : load script ¬
    (alias "Macintosh HD:Users:Smokestack:Desktop:Jimz_Handlerz.scpt")

jimzHandlerz's get_fileExtensions()

This is the actual handler containing the command I am calling from both different scenarios..

on get_fileExtensions()
    tell application "Finder"
        set theDownloadsfolder to (path to downloads folder)
        set theFiles to the name extension of every file of theDownloadsfolder
    end tell
    AST copy list theFiles without keeping duplicates -- Needs AppleScript Toolbox 2_0_8.osax Scripting Addition in /Users/"Name"/Library/ScriptingAdditions
end get_fileExtensions

So I guess my question is, are there situations I would rather use scenario one and other situations where scenario two would be better?

Update:

Here is a cool little tidbit of information: Using the scenario of calling handlers from a script library script or script bundle file as in the first example of my original question.. If the current script, that we are calling on an external script from a library, is a script bundle and we include a folder inside the “Resources” folder called “Script Libraries” containing the script or script bundle we are calling our external handlers from, if the file in the system “Script Libraries” folder cannot be found, then this script will not throw an error because it searches the resources folder also for script libraries.

enter image description here

  • 1
    I'm going to +1 just because an authoritative answer would be nice. – user3439894 Aug 5 '17 at 0:14
  • 2
    Personally, I keep a couple of files with lots of code that I reuse, via copy and paste, however, every script/app I write I prefer to make it as self-contained as possible. IMO it's just much easier to maintain when self-contained and I don't need to add additional code to make sure external sources are available, etc. I'm also not writing anything of size where I'll see a benefit to making it a multi-file scenario beyond the default files created and a custom .icns file. If I write something dependent on an third party executable, e.g. cliclick, a copy of it goes into the Resources folder. – user3439894 Aug 5 '17 at 0:14
  • I think I am leaning towards agreeing about the copying and pasting. I'm finding that if it's been a while since I have looked at the script and it calls handlers from another file, I am clueless what the commands are unless I open up the second script to see what command i'm calling. – wch1zpink Aug 5 '17 at 0:21
1

I have added a “script logger” to the beginning of every one of my scripts, which basically writes to file…. The script name and the time and date it was last run.

All of the commands for the logging, reside in a script object located in a separate file “Jimz_Handlerz.scptd” used as a script library which is located in my script libraries folder.


This following code, which is located in the file “Jimz_Handlerz.scptd”, is the script object which handles the logging

script scriptRunLog2
    try
        writeToFile()
    end try

    on writeToFile()
        set currentDate to (current date) as string
        -- The "." At The Beginning Of The Filename In The Next Line
        -- Makes That File A "Hidden" File
        set theFile to (path to desktop as text) & ".script_run_log.txt" 
        set theFile to POSIX path of theFile
        set myName to name of (info for (path to me))
        set theText to myName & " was last run on " & currentDate
        try
            set writeToFile to open for access theFile with write permission
            write theText & linefeed to writeToFile as text starting at eof
            close access theFile
        on error errMsg number errNum
            close access theFile
            set writeToFile to open for access theFile with write permission
            write theText & linefeed to writeToFile starting at eof
            close access theFile
        end try
    end writeToFile
end script

There are two ways of which I can run the commands within that script object, from any other script file.

OPTION 1 Using the "load script" option inserted at the top of any other script file

property theLibrary : (path to home folder as text) & "Library:Script Libraries:Jimz_Handlerz.scptd"
property jimzHandlerz : load script alias theLibrary
run jimzHandlerz's scriptRunLog2

OPTION 2 Using the "use" option inserted at the top of any other script file. Because "Jimz_Handlerz.scptd" is located in my script libraries folder, I do not need to define its location

use jimzHandlerz : script "Jimz_Handlerz.scptd"
run jimzHandlerz's scriptRunLog2

In regards to the question in my original post…

if I use option 1, the script logger will log which script I ran along with the time and date.

However, if I use option 2, the script logger will log "Jimz_Handlerz.scptd" as the script running the code.

In short, in this scenario, it would be best to use option 1

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .