Replace SMB address to go to file/folder location on Mac - macos

I'm on MacOS and I'm regularly sent file paths in the format of "smb://196.168.1.13/filepath/filename". I need change it to a more friendly "/Volumes/filepath/filename".
I've been searching through here for an AppleScript solution but I can't find a one that I can get to work.
Complete noob so please be gentle.
[edit] I think I cracked it. The script checks mounts the volume if necessary, converts the file path to a more friendly MacOS one then opens the file/folder location.[/edit]

Finally cracked it. Takes the smb address (mounts the volume if needed) and opens the location of the file/folder.
This is specific to our server. Change the address to your setup.
on run
--get the clipboard info
set Storage to get the clipboard
--Mount the volume if required
if Storage contains "smb://192.168.1.13/" then
set findString to "smb://192.168.1.13"
set theVolume to "/Volumes/"
set mountedVolumes to every paragraph of (do shell script "mount | awk -F' on ' '{print $2}' | awk -F' \\\\(' '{print $1}'")
if theVolume is in mountedVolumes then
--Volume is mounted
else
mount volume "smb://192.168.1.13/WIP"
end if
end if
--Convert path to mac friendly path
set replacementString to "/Volumes"
set newPath to do shell script "sed 's|" & quoted form of findString & "|" & quoted form of replacementString & "|g' <<< " & quoted form of Storage
--Open the file/folder location
set myFolderLocation to newPath
tell application "Finder"
reveal POSIX file myFolderLocation as text
end tell
end run

Related

Applescript to copy folder with specific name to another location

I've got a folder being generated with today's date with another AppleScript script, and I'm now wanting to copy that folder with today's date to another hard drive. I'm very new to AppleScript and am trying to figure out how to copy a folder from one hard drive to another, given the file name changes every time the day changes.
The code I'm using to create the folder with a specific date is below.
tell application "System Events"
delay 0.5
do shell script "date +\"%m.%d.%y\""
keystroke result
delay 0.5
keystroke return
end tell
This script should address your basic request. It gets the current date and builds a source and destination path with it. It then uses the ditto command to copy the contents of the source directory to the corresponding destination directory. If the destination does not yet exist, it will create it. This should include any sub-directories as well.
use scripting additions
-- do shell script "date +\"%m.%d.%y\""
set date8 to do shell script "date +\"%Y.%m.%d\""
set srcRoot1 to path to desktop as text
set srcRoot2 to POSIX path of srcRoot1
--> /Users/username/Desktop/2022.10.04/
set destRoot1 to path to documents folder as text
set destRoot2 to POSIX path of destRoot1
--> /Users/username/Documents/2022.10.04
do shell script "ditto -V " & quoted form of (srcRoot2 & date8 & "/") & space & quoted form of (destRoot2 & date8) & " 2>&1"
--> "ditto -V '/Users/username/Desktop/2022.10.04/' '/Users/username/Documents/2022.10.04' 2>&1"
The results above show the shell command after it is resolved. There are two optional components here. The -V option prints a list of copied files to stderr. The closing 2>&1 sends stderr to the script result where it can be seen.

Automator apple operation not permitted while trying to access volumes

I'm trying to write an Automator that outputs a .csv that represents all the directories and subdirectories with relative disk usage in an external drive, the problem is I only get "operation not permitted" even if I'm under sudo.
here is my appleScript:
on run {input, parameters}
set volumename to first item of input
set filename to second item of input
do shell script "sudo du -h /Volumes/" & volumename & "/ > " & quoted form of (POSIX path of (path to desktop)) & "" & filename & ".csv" with administrator privileges
end run
I've already tried to give full disk permissions to my test.app but doesn't work.

Exit an Applescript after script execution

I am a total Applescript newbie - mostly I work by copying examples.
I have created a simple droplet that uses the image mounter built into Toast to mount disk images that are dropped on the droplet without actually launching the full Toast program. The droplet works, but I would like the script to quit once the disk images are mounted. (As things stand now, the script application becomes unresponsive shortly after the images are mounted, but sometimes it doesn't quit when the images are dismounted.) I searched the forum and figured out I'm supposed to do a "redirect" using > /dev/null 2>&1 &, but I can't get the syntax right.
May I please have some help - Thanks!
on open image
set mount to "/Applications/'Toast 11 Titanium/Toast Titanium.app'/Contents/MacOS/ToastImageMounter"
repeat with path in image
set mount to mount & space & quote & POSIX path of path & quote
end repeat
do shell script mount
end open
EDIT: I solved it, but I'm sure this isn't the most elegant solution, so I'd appreciate feedback.
on open image
set mount to "/Applications/'Toast 11 Titanium/Toast Titanium.app'/Contents/MacOS/ToastImageMounter"
set foo to space & "> /dev/null 2>&1 &"
repeat with path in image
set mount to mount & space & quote & POSIX path of path & quote & foo
end repeat
do shell script mount
end open
I'd change a couple things. First, why do you have single quotes in your path to the toast titanium mounter? It doesn't make sense because you want to quote the whole path, not just a small portion of it. Note that in applescript we have "quoted form of" to place the quotes properly around stuff so I used that in 2 places of your code. Second, the point CRGreen makes about variable names is valid so be careful just to avoid unnecessary issues. I'd change both mount and path. Finally, you want to add foo one time at the end of the command instead multiple times inside the repeat loop.
As such here's how I would write your code. Good luck.
set mountCMD to quoted form of "/Applications/Toast 11 Titanium/Toast Titanium.app/Contents/MacOS/ToastImageMounter"
set foo to space & "> /dev/null 2>&1 &"
repeat with thisPath in image
set mountCMD to mountCMD & space & quoted form of POSIX path of thisPath
end repeat
do shell script mountCMD & foo

Path to application without opening it

I want to get a path to any application, and this is what I do:
set i to path to application id "com.adobe.Photoshop"
This gets me the path but also opens Photoshop. How can I make it so it doesn't open Photoshop?
Here's one way... use lsregister which uses Launch Services. This gives a list of all matching apps.
set lsRegisterPath to "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"
set appBundleID to "com.adobe.Photoshop"
-- get the path to all apps with the bundle id
set theAppPaths to paragraphs of (do shell script lsRegisterPath & " -dump | grep --before-context=2 \"" & appBundleID & "\" | grep --only-matching \"/.*\\.app\"")

Delete file without playing sound in Applescript?

tell application "Finder"
set deletedfile to alias "Snow Leopard:Users:test.pdf"
delete deletedfile
end tell
The problem is I repeatedly call this script from my Cocoa application so the sound is played repeatedly too. Is it possible to disable that sound ?
Since the trash is just an invisible folder inside your home folder you can do this...
set myFile to (path to desktop folder as text) & "myFile.txt"
set trashFolder to path to trash folder from user domain
do shell script "mv " & quoted form of POSIX path of myFile & space & quoted form of POSIX path of trashFolder
one simple way (doesn't move to Trash)
do shell script "rm '/Users/test.pdf'"

Resources