1

I'm working on an AppleScript to mount our Active Directory Home Shares and I've come up with the following part that looks at the logged in users "SMBHome:" and it gives me the users home share but the slashes are in the wrong direction "\" and need to change them to "/".

So far the AppleScript is:

set username to do shell script "whoami"

set homepath to do shell script "dscl '/Active Directory/CAMPUS/All Domains/' -read /Users/" & username & " |grep SMBHome:"

When run this gives me an output of

"SMBHome: \\\\server\\share\\username"

I'd love to have the next step replace the back slashes with forward slashes and remove:

"SMBHome: \\"

With a final outcome of:

//server/share/username

Is this possible?

Thank you!

2 Answers 2

1

There are two approaches to solving this problem. One is to use JavaScript for Automation (either by putting your AppleScript into an Automator workflow and then using a separate Run JavaScript action, or by re-writing the entire script in JXA), while the other is to use the sed and cut command line utilities via do shell script.

Here's the JavaScript approach:

var string = "SMBHome: \\\\server\\share\\username" // this would be your 'homepath' variable
var corrected = string.replace(/\\/g, '/').replace('SMBHome: ', '')

This script uses the built-in JavaScript replace function to match and replace text using Regular Expressions.

On the other hand, you could also use sed and cut in your AppleScript, like so:

set init to do shell script "echo ' " & homepath & "' | sed 's/\\\\/\\//g'"
set corrected to do shell script "echo " & init & " | cut -c 10-"

This uses sed for the RegEx, then cut to trim the first bit of the path from the string.

In both cases, the corrected variable contains the final, modified string. I should note that, while the desired behavior can be accomplished in pure AppleScript, it can be rather tedious.

0

Thanks for the reply @aaplmath, I actually just found the needed addition which is what you have and it does the job!

| grep SMBHome: | cut -c 10- | sed 's/\\\\/\\//g' "

Thank you for your input!

0

You must log in to answer this question.

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