BigLaser Posted August 22 Share Posted August 22 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
vitor Posted August 22 Share Posted August 22 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
BigLaser Posted August 22 Author Share Posted August 22 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}". Exact error - "A privilege violation occurred." I would expect this to be reproducible with any valid SMB path a tester would have. 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. M2 Macbook Pro Sonoma 14.6.1 Alfred 5.5 [2257] 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. 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
vitor Posted August 22 Share Posted August 22 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) BigLaser 1 Link to comment
BigLaser Posted August 22 Author Share Posted August 22 That's great, it works. Thank you for the assistance. How did you sleuth this? Which pieces of documentation were most valuable with this? I'd like to be self sufficient with documentation when possible, but don't know where to look vitor 1 Link to comment
BigLaser Posted August 22 Author Share Posted August 22 Would this be the best resource? https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/conceptual/ASLR_fundamentals.html#//apple_ref/doc/uid/TP40000983-CH218-SW3 Link to comment
vitor Posted August 23 Share Posted August 23 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. BigLaser 1 Link to comment
Stephen_C Posted August 23 Share Posted August 23 7 minutes ago, vitor said: often it may not be exactly obvious how to make something work You can say that again! (Sorry, couldn't resist the comment.) Stephen vitor and BigLaser 2 Link to comment
BigLaser Posted August 26 Author Share Posted August 26 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now