How to pass variable from applescript to km

LOL… I found it… Thank you… :smile:
Again Thank you… :smile:

I think this could be smaller... What do you think ccstone?
I had a problem with the last one it would try to mount volumes over and over, this way it checks first but I think it's fat...

tell application "Finder"

if not (disk "FaceBook Pictures" exists) then
mount volume "afp://192.168.2.101/FaceBook Pictures" as user name "admin" with password "mypassword"
display notification "FaceBook Pictures Mounted"
end if

if not (disk "Art work Files" exists) then
mount volume "afp://192.168.2.101/Art work Files" as user name "admin" with password "mypassword"
display notification "Art work Files Mounted"
end if

if not (disk "Download" exists) then
mount volume "afp://192.168.2.101/Download" as user name "admin" with password "mypassword"
display notification "Download Mounted"
end if

if not (disk "Mac Documents" exists) then
mount volume "afp://192.168.2.101/Mac Documents" as user name "admin" with password "mypassword"
display notification "Mac Documents Mounted"
end if

if not (disk "Movies" exists) then
mount volume "afp://192.168.2.101/Movies" as user name "admin" with password "mypassword"
display notification "Movies Mounted"
end if

if not (disk "Qsync" exists) then
mount volume "afp://192.168.2.101/Qsync" as user name "admin" with password "mypassword"
display notification "Qsync Mounted"
end if

if not (disk "Stuff" exists) then
mount volume "afp://192.168.2.101/Stuff" as user name "admin" with password "mypassword"
display notification "Stuff Mounted"
end if

if not (disk "Rachel Files" exists) then
mount volume "afp://192.168.2.101/Rachel Files" as user name "admin" with password "mypassword"
display notification "Rachel Files Mounted"
end if

if not (disk "USBDisk1" exists) then
mount volume "afp://192.168.2.101/USBDisk1" as user name "admin" with password "mypassword"
display notification "USBDisk1 Mounted"
end if

if not (disk "USBDisk2" exists) then
mount volume "afp://192.168.2.101/USBDisk2" as user name "admin" with password "mypassword"
display notification "USBDisk2 Mounted"
end if

if not (disk "Multimedia" exists) then
mount volume "afp://192.168.2.101/Multimedia" as user name "admin" with password "mypassword"
display notification "Multimedia Mounted"
end if

end tell

Hey Bill,

I reckon.

There's nothing wrong with that methodology, except that it becomes more and more difficult to maintain as the list gets longer.

So let's change things around and make it easy to maintain.

I don't have a network here to test with, so you'll have to be the guinea pig.

------------------------------------------------------------
try
  
  set networkDisks to text 2 thru -2 of "
afp://192.168.2.101/Art work Files
afp://192.168.2.101/Download
afp://192.168.2.101/FaceBook Pictures
afp://192.168.2.101/Mac Documents
afp://192.168.2.101/Movies
afp://192.168.2.101/Multimedia
afp://192.168.2.101/Qsync
afp://192.168.2.101/Rachel Files
afp://192.168.2.101/Stuff
afp://192.168.2.101/USBDisk1
afp://192.168.2.101/USBDisk2
"
  tell application "Finder" to set diskNameList to name of disks
  set AppleScript's text item delimiters to "/"
  repeat with i in paragraphs of networkDisks
    set {diskAddress, diskName} to {contents, last text item} of i
    if diskName is not in diskNameList then
      # mount volume diskAddress as user name "admin" with password "mypassword"
      display notification diskName & " Mounted"
    end if
  end repeat
  
on error e number n
  set e to e & return & return & "Num: " & n
  if n ≠ -128 then
    try
      tell current application to button returned of ¬
        (display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
          default button "OK" giving up after 30)
      if ddButton = "Copy" then set the clipboard to e
    end try
  end if
end try
------------------------------------------------------------

I've left the mount line commented, so you'll need to uncomment it when you're ready to try it out.

This kind of table-based system is much easier to work with and maintain than a bunch of If-statements or AppleScript records. I've used similar methods for many tasks over the years, although I usually use the Satimage.osax AppleScript Extension and regular expressions to parse tables.

-Chris

1 Like

Bump — fixed some typos and added basic error-checking to the script.   -ccs

1 Like

WOW it worked perfectly and Much leaner! :smile:
I’ll look at this line by line to understand how it works never stop learning Loving it.
I wonder is there a resource for applescript for dummies? LMAO…

You are very kind to helping out.
Once again Chris thank you very much. :smiley:

Hey Bill,

There is such a book.    :smile:

Although I haven't had my hands on it and don't have an opinion about its worth.

This might interest you:

References for Learning & Using Applescript

-Chris

1 Like

I found this book very helpful:

Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X (Learn (Apress))

Chris, thanks for creating and sharing such a tremendous resource.

JMichael

Good list of references !

Stepping back a bit, perhaps the key is really to get a clear grasp and practiced familiarity with the core concepts – after that the syntax of any particular language is really just a variation in detail.

My list of the core concepts would probably be:

  • What is a variable, really, and what is going on in assignment of values to variable names ?
    (and what kind of values are there ? Numbers, strings, true|false ‘booleans’, and lists, for a start)
  • How do true | false (‘boolean’) values work in combination with operators like AND, OR, NOT ?
  • How can one break a problem down into a nest of sequences, branches and loops ?

and then how can you:

  • hide details with ‘sub-routines’ ?
  • apply a function to a value and collect the function’s result ?

and for extra credit

  • how do you write a function whose definition includes itself ?
  • why would you want to ?
  • how can you use a function as the value of a variable ?
  • and why would you want to do that ?

And for context:

What does Lisp look like, and how, broadly, does it work ?

( There is a sense in which all language translators/interpreters rewrite to something very much like Lisp before rewriting down to machine code, and while a dialect like Applescript is relatively niche and ephemeral, we can be fairly confident that even in other galaxies the deep structure of coding is bound to be Lisp-like. See section 9 (conclusion) of Phil Wadler’s paper )

Expertise is not so much knowing the details as grasping the fundamentals, and the best introductions to Applescript might even be books like:

https://mitpress.mit.edu/books/little-schemer on Lisp

or http://eloquentjavascript.net/ on Javascript

( The concepts are the same, and once you have those, the details will come easily : -)

2 Likes

Following on from @ComplexPoint’s, err, point, one of the best ways to learn basic programming I’ve seen recently is the Hour of Code, which teaches these sorts of fundamentals. It happens that they use JavaScript, which is very handy for web programming, and now becoming very handy for Mac automation programming as well.

2 Likes

I'm not sure that I agree. :wink:

I find the so-called "natural language" of AppleScript to actually work against the fundamental knowledge I have of programming, and the many years of programming in other languages. Since I have never thought about coding in a natural, english-language like sense, I find AppleScript to be counter-intuitive.

If a person has never programmed before, of course they need to learn the basic concepts of program design. But learning another language that is nothing like AppleScript might be more of a hindrance than help.

It seems to me that few of the terms used in AppleScript are used elsewhere, and most of the terms common in other languages (and even design) are not used in AppleScript.

So, IMO (and I am far from being an AppleScript expert), a person with no programming experience is better off starting with AppleScript focused training, be that books, tutorials, videos, or other.

As Chris mentioned, much of learning AppleScript is learning the unique code for a specific application. For me, the fastest way I have learned is by example, inspecting, testing, code written by others.

Just my 2¢.

Yes, I do think AppleScript’s well-intentioned “Englishness” is inadvertently obfuscating, although I’ve had a lot of fun using it over the years.

As you already have all the concepts in place, it might be worth experimenting with JavaScript for Applications once you install Yosemite. It feels in a way slightly late in the day to start putting a lot of work into making sense of Applescript, especially given the broken or defective nature of its record type, and its dependence on 3rd party fixes for some library basics like regexes.

( AS records can’t tell you what keys/fields they’ve got, and raise errors if you test them to find out )

Thanks for the suggestion. I will most likely dive into JXA as soon as I upgrade to Yosemite.

Is JFA intended to be a full replacement, or alternate, to AppleScript?
IOW, should I be able to do anything in JXA that I can do now in AppleScript?
Does JXA use, or access, the AppleScript API provided by an app?

Yes, they use exactly the same library interfaces – it's just a set of alternate syntaxes for accessing them. Anything reachable by AS is equally reachable by JXA.

The Yosemite .sdef library viewer now allows you to switch between three viewing syntaxes:

https://developer.apple.com/library/mac/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/

That's great! Many thanks.

Rather than continue our off-topic discussion about learning AppleScript and JXA here, I have started a new topic. Please continue your excellent posts here:

Learning & Using AppleScript & JavaScript for Automation (JXA)

That is at least the intention.

Following other folks' efforts to use JXA since its inception I've noted that a number of things were (or still are) broken, however Chris Nebel and Chris Page at Apple have been working hard at improving AppleScript infrastructure — so I expect things to keep improving.

-Chris

Yes, I am sure it will continue to develop, but in my experience ver 1.0 is already an excellent resource. FWIW I have written a fair amount of it by now without hitting any glitches : -)

( The NSXML access took me a while to figure out :- )

( You can see a range of explorations in the JavaScript for Automation Cookbook wiki )

Chris, could you share the specific things that are broken?

Hey Michael,

Since I'm still using Mavericks and for that matter know only a smattering of JavaScript I've only paid attention in passing.

Once I upgrade my system I'll start doing more JavaScript and take JXA for a personal test drive.

-Chris