Path to application without opening it - applescript

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\"")

Related

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

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

How to automatically rename files when they get into folder

I've been trying to find a program or some sort of app to run all the time and check one folder. If any document would be saved in the folder it would rename it like file1.png the next one file2.png.
Transnomino has an automation feature that allows to automatically rename any file dropped in a folder using a specified renaming Recipe. The Apple Script that does this looks something like this:
on adding folder items to theAttachedFolder after receiving theNewItems
tell application "Finder" to set thePath to POSIX path of theAttachedFolder
return do shell script ¬
"open -a Transnomino --args -d \"" & thePath & "\" -r \"" & thePath & ".recipe\""
end adding folder items to
A complete guide how this can be setup is described on the website of Transnomino. Here: https://transnomino.bastiaanverreijt.com/automation/index.html

Automator could not access required data

I want to run an Automator workflow from within an AppleScript. When I use Terminal I can do:
/usr/bin/automator -i /Users/.../filename /Users/.../worfklowname.workflow/
But when I do this as part of the AppleScript via
do shell script "/usr/bin/automator -i /Users/.../filename /Users/.../worfklowname.workflow/"
I get an error stating that Automator could not access the required data. What's wrong here?
Thank you!
I found a solution:
set startpath to "Macintosh HD:Users:...:filename"
set workflowpath to "Macintosh HD:Users:...:workflow"
set qtdstartpath to quoted form of (POSIX path of startpath)
set qtdworkflowpath to quoted form of (POSIX path of workflowpath)
set command to "/usr/bin/automator -i " & qtdstartpath & " " & qtdworkflowpath
do shell script command
It's probably due to all the entanglements that this solution also looks a bit non-straightforward :-D

Avoid launching when setting variable to path to applet

If I set a variable in AppleScript to the path to an AppleScript applet or droplet, the applet or droplet is launched and the run() handler is executed. For example, I have
set thePath to path to application "HS Extract"
and this launches the droplet "HS Extract". I have also tried setting a variable to the application as alias and then setting a variable to the path to the alias and a few other possibilities that all failed.
How can I set a variable to the path to a droplet or applet without launching that droplet or applet?
Another way to find things is using mdfind from the shell and metadata properties. This is like doing a spotlight search and this method shouldn't launch any applications.
For example applications have the kMDItemContentType property of com.apple.application-bundle. If we combine this with the name of an application we can create something like the following to get a posix path and then convert it into an alias.
set appDisplayName to "HS Extract"
set posixPath to item 1 of paragraphs of (do shell script "mdfind \"kMDItemContentType == 'com.apple.application-bundle' && kMDItemDisplayName == " & quoted form of appDisplayName & "\"")
set thePath to (POSIX file posixPath) as alias
NOTE: you mention the bundle id is common in your comment to jackjr300's post. The bundle identifier has the kMDItemCFBundleIdentifier metadata property. So if the combination of kMDItemContentType and kMDItemDisplayName doesn't work (as in my example code) then you can try kMDItemCFBundleIdentifier and kMDItemDisplayName instead.
To not open an application : use the Finder to get the path, this need the bundle identifier of the application.
set bundlId to id of application "HS Extract"
tell application "Finder" to set thePath to (application file id bundlId) as alias
Or you can use a NSWorkspace method to get the path, this doesn't need an identifier
do shell script "/usr/bin/python -c 'from AppKit import NSWorkspace; print NSWorkspace.sharedWorkspace().fullPathForApplication_(\"HS Extract\")'"
set thePath to the result as POSIX file as alias
What about setting it as a string, then, if you need to launch it, coercing to alias.
Set thePath to (choose file as string)
--gives you path string
you can use that string in a variable, but use
alias thePath
if you need to.
I prefer #jackjr300's answer, but if this script is for private use, a property seems to work:
property appPath : path to application "HS Extract"
display dialog appPath as string
The application is only launched when the script is compiled. Note that the property is an alias.
The script will track the original HS Extract, even if it's later renamed to something else.

File associations in OS X: doesn't pass filename in args?

I am packaging an application into a .app directory for "drag install" or whatever it's called and I have a weird iessue with file association.
I set my application as a viewer for .xyz files, and the system does start my app when I double click that file; the only problem is that the path of the file I clicked is nowhere in the args[], there's only one parameter that is something like ~psn_0_901340 and I think is a timestamp because it changes every time.
So... what am I supposed to do? I've been sitting here for 2 hours straight and can't find a solution.
I think what you want is an AppleScript droplet.
A shortened version of the AppleScript from that link:
on open dropped_files
set the_command to quoted form of POSIX path of (path to resource "script.sh")
set file_list to ""
repeat with file_path in dropped_files
set file_list to file_list & " " & quoted form of POSIX path of file_path
end repeat
set the_command to the_command & file_list
do shell script the_command
end open
Export as an application using Script Editor. Place script.sh in the Resources folder.
Add your file extension associations to Info.plist. You may need to launch or move the droplet before OS X notices the change & allows you to double-click files.
If you want to launch Terminal or capture the script output, see the full AppleScript.

Resources