Avoid launching when setting variable to path to applet - applescript

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.

Related

AppleScript not complying due to image

I'm using below code to load an image:
alias ((path to me) & "Contents:Resources:FCPXporter.icns" as string))
But I'm getting the error:
error "File alias Macintosh HD:Users:apple:Downloads:FCPXporter_Version_3.1.scptContents:Resources:FCPXporter.icns of «script» wasn’t found." number -43
How do I fix it?
The recommended syntax is
alias ((path to me as string) & "Contents:Resources:FCPXporter.icns")
It handles the path separators reliably.
But your script doesn't have a Resources folder because it has been saved as regular compiled script.
Two possible solutions:
Save the file as script bundle (.scptd), put the icon in the – now present – Resources folder and use the code above.
If the icon is supposed to be on the same level as the script use
set myself to path to me
tell application "System Events" to set parentFolder to (path of container of myself)
set theImage to (parentFolder & "FCPXporter.icns") as alias
In your code, your script file has the extension ".scpt" but your script file needs to be saved as either a script bundle (.scptd) or an application (.app) to have a "Resources" folder
set theImage to (path to me as string) & "Contents:Resources:FCPXporter.icns" as alias
-- Returns value.. alias "Macintosh HD:Users:apple:Downloads:FCPXporter_Version_3.1.scptd:Contents:Resources:FCPXporter.icns"
It looks like you are missing a path separator - try changing:
alias ((path to me) & "Contents:Resources:FCPXporter.icns" as string))
to
alias ((path to me) & ":Contents:Resources:FCPXporter.icns" as string))
If that still doesn't work then check that the file actually exists at the specified location - go to Terminal and:
ls "Macintosh HD/Users/apple/Downloads/FCPXporter_Version_3.1.scpt/Contents/Resources/FCPXporter.icns"

Selecting POSIX file based on file name

I'm having trouble accessing this file while trying to select it on the beginning characters basis...
set location to "/Users/myuser/Desktop/"
set bom to POSIX file (location & (first file of location whose name begins with "thisFile"))
tell application "Preview" to open bom
is it path/alias vs text type of a thing?
Only System Events and the Finder know what a file in the file system is.
The Finder has a property desktop which points always to the desktop of the current user.
tell application "Finder" to set bom to first file of desktop whose name begins with "thisFile"
tell application "Preview" to open (bom as alias)
Or with an arbitrary POSIX path
set location to POSIX file "/Users/myuser/Desktop" as text
tell application "Finder" to set bom to first file of folder location whose name begins with "thisFile"
tell application "Preview" to open (bom as alias)
The alias coercion is needed because Preview doesn't recognize Finder file specifier objects.
vadian's answer works well, but it's worth mentioning that:
you can get access to well-known folders even in the default context, outside the context of System Events and Finder; e.g.:
path to desktop
path to home folder
Use, e.g., POSIX path of (path to home folder) to get the POSIX path.
using context System Events is usually preferable to the Finder context, for reasons of both performance and predictability.
With an arbitrary target folder, using a POSIX path:
tell application "System Events"
set targetFolder to alias "/Users/jdoe/Desktop"
# equivalent of: set targetFolder to (path to desktop)
set targetFile to first file of targetFolder whose name starts with "thisFile"
end tell
tell application "Preview" to open targetFile
Alternatively, if you know your way around the shell, you could try:
set targetFilePosixPath to do shell script "fls=(~/Desktop/*.pdf); printf %s \"$fls\""
tell application "Preview" to open (POSIX file targetFilePosixPath as alias)

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.

Applescript run bash script from same directory

I'm trying to build an AppleScript to launch my shell script.
Path structure is as follows
/Users/ryan/myscript/
applescript.scpt
bash.sh
My AppleScript is as follows:
tell application "Terminal"
set folder_path to path to me
set run_cmd to "/bin/bash " & folder_path & "/bash.sh"
do script run_cmd
activate
end tell
Problem is the 'path to me' is not always returning the correct path. When executed using the Mac cd/dvd autoplay behavior folder_path is equal to:
disk:System:Library:CoreServices:SystemUIServer.app:Contents:XPCServices:com.apple.systemuiserver.scriptrunner.xpc:
Is there is a better way of getting the folder path?
If this Script is in a static location, you can do this:
do shell script "/bin/bash" & POSIX path of (path to current user folder) & "myscript/bash.sh"
Path to me refers to the location of the applescript that is running. So if your script is on a disk then it will reference the location on the disk where the script is saved
if it is expected that the shell script will always exist in a folder called "myscripts" that exists in the current user folder then you could use path to current user folder and build out from there
set user_folder to path to current user folder
set folder_path to quoted form of POSIX path of (("" & user_folder & "myscript"))
tell application "Terminal"
activate
set run_cmd to "/bin/bash " & folder_path & "/bash.sh"
do script run_cmd
end tell
Is there a reason why you have to store the shell script in a separate file? Typically, you would put it inline, within the AppleScript code. As far as I know, the “do shell script” command only operates on text, not on a script at a file path. If you give it a variable that contains a path, it will try to run that path as a command. It won’t run the contents of the file as a command.
Here is an example of an AppleScript that runs an inline shell script and puts the results in TextEdit:
property theShellScript : "#!/bin/bash
echo Hello World"
tell application "TextEdit"
activate
set theScriptResult to do shell script theShellScript
make new document
set the text of document 1 to theScriptResult
end tell
… you can of course replace the above shell script with the contents of your own shell script.
If you do need to keep the script in a separate file, the best way to do that is probably to save your AppleScript as an Application, and put the shell script within the Application bundle. “Path to me” is the path of the application that is running the script — not to the script itself — but if you save your AppleScript as an Application, then it runs its own script, and “path to me” works as you originally expected.
Here is an example of an AppleScript that runs a shell script contained within a file that is stored within its own application bundle:
property theApplicationPath : the path to me as text
property theShellScriptPath : theApplicationPath & "Contents:Resources:Scripts:bash.sh"
tell application "TextEdit"
open alias theShellScriptPath
set theShellScript to the text of document 1
set theScriptResult to do shell script theShellScript
make new document
set the text of document 1 to theScriptResult
end tell
With the above script Copy/Pasted into a new document in AppleScript Editor, hold down the Option key and choose File ▶ Save As, and in the Save dialog box, on the File Format pop up menu, choose “Application” and of course give your application a name and click Save. Then in Finder, navigate to where you Saved your application, and 2-finger tap (or right-click) on your application and choose “Show Package Contents.” That opens your application up as a folder, exposing the file system within. Put your shell script file named “bash.sh” inside the folder “Contents/Resources/Scripts” within your application and then close the window that represents your application.
Now when you run your application from anywhere in the file system, it will still be able to find and run its incorporated shell script.

Applescript to Duplicate Files using My own application

I'm developing a Mac OS X application and in some case it needs to copy a file to /Library/ScriptingAdditions.
And using the code below
tell application "Finder"
duplicate sourcePath to destinationPath with replacing
end tell
will prompt a dialog saying "Finder" wants to make some changes...
I would like to make the dialog saying My Application wants to make some changes....
I've read about https://developer.apple.com/library/mac/documentation/security/conceptual/authorization_concepts/01introduction/introduction.html
but it doesn't seem to work with AppleScript.
If you would use the shell instead of AppleScript, the dialog asking for the password will display the name of your own application. Here's an example that copies the file "_this is a test.xyz".
set sourcePath to "'~/desktop/_this is a test.xyz' " -- mind extra space
set destPath to path to scripting additions folder -- change this to your destination folder
set destPath to POSIX path of destPath
set destPath to "'" & destPath & "_this is a test.xyz'"
set shellScript to "cp -n " & sourcePath & destPath
do shell script shellScript with administrator privileges
Warning: don't just run this script without modification, as it will add an empty file to your scripting additions folder and you probably don't want that. This script just serves as an example to look at.
Edit:
This will only work if you can compile your app as an independent app. If you're using Python, you need to compile your Python scripts as a standalone app with a name. The password dialog will show the name of the standalone app instead of "Python".

Resources