Jump to content

JSX script encounters privilege error when AppleScript does not


Recommended Posts

I have an AppleScript which successfully mounts a networked SMB volume to the system via Finder. I rewrote it in Javascript JSX, but encounter a privilege error when running.

 

Is this expected behavior when running JSX vs AppleScript? Or perhaps there is a different way to mount a volume via JSX which handles privileges similarly to the AppleScript.

 

Apple Script

on run argv
    set dir to system attribute "pc_w"
    tell application "Finder"
        mount volume "smb:" & dir
        return "smb:" & dir
    end tell
end run

 

JSX Javascript
 

#!/usr/bin/env osascript -l JavaScript
// ObjC.import("stdlib");

function run(argv) {
    var dir = $.NSProcessInfo.processInfo.environment.objectForKey("pc_w").js;
    var finder = Application("Finder");
    finder.includeStandardAdditions = true;
    finder.mountVolume("smb:" + dir);
    return "smb:" + dir;
}

 

Link to comment
1 hour ago, BigLaser said:

Is this expected behavior when running JSX vs AppleScript?

 

So we’re on the same page, you mean JXA. JSX is something different.

 

Additionally, note Alfred has no bearing on those differences of behaviour (yes, they exist¹) as Alfred only asks the relevant macOS APIs to run the code. When something doesn’t work as expected, and barring any issue with the code, the problem will be something only Apple can solve.

 

1 hour ago, BigLaser said:

but encounter a privilege error when running.

 

When asking for assistance, it’s helpful to provide as much information as possible.

  • What is the exact error? The message could make all the difference in the diagnostic.
  • What’s your version of macOS? That often matters.
  • Does it have to be JXA or are you asking just to learn? I find JXA superior to AppleScript in general, but both have bugs the other doesn’t.¹
  • Have you tried other approaches? E.g. a simple Open URL Action pointing to smb:{var:pc_w} should work, as should a shell command to open "smb://${pc_w}".

 


¹ Including in the Finder. For example, JXA errors when telling the Finder to trash multiple paths if at least one of them in a folder. I have a bug report open with Apple about this since 2018 (FB5715339).

Link to comment
1 hour ago, vitor said:

 

So we’re on the same page, you mean JXA.

Yes, JXA. Apologies for the confusion. This is not a pressing issue. The AppleScript works as expected. This is educational, and I appreciate you taking the time to respond.

 

2 hours ago, vitor said:
  • What is the exact error? The message could make all the difference in the diagnostic.
  • What’s your version of macOS? That often matters.
  • Does it have to be JXA or are you asking just to learn? I find JXA superior to AppleScript in general, but both have bugs the other doesn’t.¹
  • Have you tried other approaches? E.g. a simple Open URL Action pointing to smb:{var:pc_w} should work, as should a shell command to open "smb://${pc_w}".
  1. Exact error - "A privilege violation occurred."
    1. I would expect this to be reproducible with any valid SMB path a tester would have.
    2. I also expect this to be a valid permission consideration (since mount points are likely being created at the root directory). However, I'd expect AppleScript and JXA to handle this the same way. It's good to know that there can be differences in behavior between JXA and AppleScript.
  2. M2 Macbook Pro
    1. Sonoma 14.6.1
    2. Alfred 5.5 [2257]
  3. It doesn't have to be JXA. I ask for educational reasons. I too prefer to use JXA where possible for reasons you and I have previously discussed. It is generally superior to AppleScript. It's good to know that there can be differences in behavior between JXA and AppleScript.
  4. Other approaches - The AppleScript works well to achieve desired behavior, and I am using that. I didn't know the Open URL Action could handle SMB, thanks for the tip!

 

Is this considered a JXA bug?

Link to comment

I looked at the AppleScript dictionaries and mounting volumes is not really a Finder thing, but a Standard Additions thing. Looks like the problem is that you’re trying to elevate those for the Finder. Instead, do:

 

const dir = $.NSProcessInfo.processInfo.environment.objectForKey("pc_w").js
const app = Application.currentApplication()
app.includeStandardAdditions = true
app.mountVolume("smb:" + dir)

 

Link to comment

For AppleScript dictionaries, open the Script Editor app and do File → Open Dictionary… Or use the workflow. Note that while the dictionaries are invaluable to understand everything that’s available, often it may not be exactly obvious how to make something work. But there is a “method to the madness” so it’s something you can begin to figure out with practice.


I looked in the Finder’s dictionary first, and when “mount” produced no matches I tried the Scripting Additions dictionary.


I also found the section on Scripting Addition Security and remembered Application.currentApplication() is the way to go. I don’t recall when I first learned that, though.

Link to comment
On 8/23/2024 at 6:11 AM, vitor said:

often it may not be exactly obvious how to make something work. But there is a “method to the madness” so it’s something you can begin to figure out with practice.

 

Thank you for the link, as well as that statement. I have used this documentation before, but forgot it. I also remember the frustrations of this documentation. Making something work is not always obvious, but it is valuable documentation when trying to figure out why an existing script works.

 

Thanks again.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...