Applescript: Application ID only working with default version - applescript

I'm working on a script for Photoshop and was using application id "com.adobe.photoshop" to identify when Photoshop is open. Unfortunately this ONLY seems to work for whatever the default version is. I have several machines this needs to go on and they might have 3 or they might have 5 versions.
I'd like it to work with whatever version is currently open.
The only way I can think to do that is to have an initial check of
if application "Photoshop CS6" is running
set Appname to "Photoshop CS6
ect... ect... ect...
for each version, but that's messy and if the version doesn't exist it generates a popup asking for where that is located.
Any ideas? =/

if you are sure that Photoshop is already launched at the time you are running your script, you can do the check by looking to all running processes as bellow :
tell application "System Events" to set PShop to name of every process where name contains "Photoshop"
set AppliName to item 1 of PShop
if Photoshop is not running at that time, I suggest you to have a script which looks to every files of folder Application whose name contains Photoshop.

Related

How to remove mapping/binding of application in AppleScript

I have some AppleScript which I run via osascript on the command line.
The script itself looks like:
on run argv
set appname to item 1 of argv
set tmp to item 2 of argv
set jsfl_path to POSIX file tmp
if application appname is running then
tell application appname
with timeout of 600 seconds
open jsfl_path
end timeout
end tell
end if
end run
I updated Adobe CC Animate to the latest version. I was previously running the 2018 version. It turns out they renamed the file pattern. It used to be Adobe Animate CC 2018. Now it is Adobe Animate 2019.
Here is where the problem starts. In my script I was sloppy and change the name to Adobe Animate CC 2019.
When I ran the AppleScript, it produced a dialog to Choose Application. In my haste, I accidentally mapped it to the wrong program.
I'd like to remove this mapping. What I cannot find is where this mapping is stored. Does anyone know where this type of mapping gets saved?
It isn’t really stored anywhere, the Script Editor uses your declarations to determine where to load the scripting terminology from. Recompiling the script after making an edit should reset things, otherwise you can restart the Script Editor. Note that you need to use a specific bundle identifier or application name - using a variable to specify the application can be problematic, as the terminology is loaded at compile time.

Bring application to front by its path

How can I bring an application to front by its path?
i.e. Assume /Applications/MyApp.app have already started; At a time I want to bring that MyApp.app window to front by passing its path to AppleScript: myApplScript.scpt /Applications/MyApp.app.
I tried by this script, but this did not worked for me:
on run argv
set apppath to (item 1 of argv) as string
tell application "System Events"
set frontmost of every process whose path is apppath to true
end tell
end run
Thanks!
Usually, all you need to do is to activate the application, which switches focus to it (even if the application is already running):
activate application "MyApp"
You can use this command directly with its path like this:
activate application "/Applications/MyApp.app"
or, in your specific case,
activate application apppath
although you shouldn't need to.
If that doesn't work, you can try System Events:
tell application "System Events" to set frontmost of process "MyApp" to true
or, using its path:
tell application "System Events" to set the frontmost of the first process ¬
whose POSIX path of application file is "/Applications/MyApp.app" to true
Have you tried checking for the open files for the process?
There are several ways of doing this (see: this article)
Normally the application binary will be an open file of the process.
Keep in mind that the binary executable may be contained in a package and you will need to look for it by doing a show package contents in finder and then appending the path to the executable in the package. In the package check under /Contents/MacOs/.
This might take a long time if you have many open processes and therefore not very practical to run often.
Also take into account that a single executable file can be running in more then one process instance.
A pity that the standard Activity Monitor is not scriptable with applescript which I just verified hoping for a more applescript friendly solution. You should still be able to achieve the same result via calling the shell command line.
Also often the process name is equal or similar to the filename but that depends on the application or process you're looking for.
I'm guessing you must have different versions of the same app installed that you need to check for the application by pathname?
Or if you don't need to figure out the path name dynamically you can simply look it up in advance using the Activity Monitor or on the command line. There you can identify the process name that corresponds to you your running application that was started from the specified path. Knowing the process name in advance then makes it easy and you can simply use
tell application "System Events" to get application process "Name of My Application"
--insert the actions you want to perform on your running app here
end tell
To solve any process uniqueness issues in case the same app is running more then once you can always result to the pid or unix process Id. Here's an article on how to get the pid in applescript of a running app

AppleScript (or alternative) that checks running apps and closes the last opened instance

I have an OS X app, let's call it TestOSX.app. This is its displayed name (taken from the Info.plist CFBundleName key as far as I can tell).
For a variety of reasons (it can be circumvented by copying the app to another place or by opening it from Terminal; it does not work if CFBundleExecutable is not the binary per-se but a script that sets up some stuff before launching the binary itself...), I cannot rely on OS X's built-in policy to block someone from starting a second instance of the app, nor can I use the LSMultipleInstancesProhibited key. But I do want to make sure that every second instance started by the same user is going to quit before being able to modify some resources. Different users should be able to run their own single instace of the app at the same time (this is why LSMultipleInstancesProhibited is no-go).
(I wanted to build a mechanism relying on flock(1) but it does not exist under OS X.)
So, the strategy is: when a user launches my app, first check whether an older version is already running; if there is, send this latest app instance (that the script has been executed "from") a request to quit, and bring the old instance to foreground.
I cannot use the name of the process per-se, as the app may use some embedded tools (like a proprietary updater) which will have a different name than the app itself. This is why something like this won't work:
tell application "System Events"
set listOfProcesses to (name of every process where background only is false)
end tell
, as the identified process may simply say updater (which is a part of the TestOSX bundle).
I have a snippet, probably parts of the "big thing", but it doesn't work as expected:
tell application "System Events"
set theProcess to first application process whose displayed name is "TestOSX"
set theOtherProcess to second application process whose displayed name is "TestOSX"
set frontmost of theOtherProcess to true
end tell
, this one always brings to front only the 1st app's process.
And I don't get it why it doesn't work as expected, as long as:
tell application "System events"
set listOfProcesses to (name of every process whose (dsiplayed name is "TestOSX"))
end tell
returns both instances. I guess somewhere the mapping between the process and the name is being lost.
[Edit]
Tried to modify the snippet above using:
tell application "System Events"
set theOtherProcess to id of second application process whose displayed name is "TestOSX"
set frontmost of theOtherProcess to true
end tell
, yet I get the error:
"Can't set frontmost of 680102 to true."
(This may be because I have a script that actually launches the binary, as said above?)
Okay, I came up with an ugly solution.
The bash script that is launched by double-clicking on the app icon is going to check how many instances of my app are running (with the aid of an AppleScript). If the answer is more than one, the bash script will end, thus my whole app terminating.
My launcher.command script:
#!/bin/bash
val=$(osascript ./instances_counter.scpt "TestOSX")
and the instances_counter.scpt, making use of the argument I passed:
on run argv
tell application "System Events"
set theProcessList to every application process whose displayed name is item 1 of argv
set noOfProcesses to count of theProcessList
end tell
return noOfProcesses
end run
That's it.

Create AppleScript for a program that isn't installed on the current computer

I'm trying to make two copies of an AppleScript, one that works for Entourage and one for out Outlook. I only have Entourage installed on the current computer.
According to the info on Microsoft's site, both applications have the same library of AppleScript commands, and I should be able to simply change the application name referenced within the script.
Changing:
Tell application "Microsoft Entourage"
to
Tell application "Microsoft Outlook"
Prevents me from saving the script because I don't have outlook installed on this computer. Is there any way around this? Do I need to use a text editor to edit the actual script file and change it there?
Thanks!
The following work-around may do the trick. On the computer where Entourage is installed, a using terms directive will let you compile the script, even if Outlook is not installed:
set theApp to a reference to application "Microsoft Outlook"
using terms from application "Microsoft Entourage"
tell theApp
get version
...
end tell
end using terms from
Upon compiling and saving the script the AppleScript Editor will bug you about the missing Outlook application, but it will nevertheless produce a compiled AppleScript file (.scpt).
Applescript is a pre-complied file format, meaning that every time you click "Save" it runs through a series of steps to ensure the script will work, but just short of actually running through the script's logic. Part of those steps is to look for the application to see if it exists on the Mac.
In short, if you want to save the script as an Applescript, you need the target application installed, otherwise you can save the script as a text file and move the file over to the target Mac to save as an Applescript over there.
It should be possible to make one script that works with both Entourage and Outlook, without bugging you if one isn't found either when you compile or when you run. I don't have either Entourage or Outlook but it should work like this:
using terms from application "Microsoft Entourage"
script theScript
tell application "Finder" to try
set theApp to application file id "Entourage's Bundle ID" as text
on error
set theApp to application file id "Outlook's Bundle ID" as text
end try
tell application theApp
-- do stuff
end tell
end script
end using terms from
store script theScript in "MyScript.scpt"
"using terms from" is only relevant when compiling the script - it isn't needed when running, though for some reason you'll still get bugged if that app isn't found. So by wrapping it around a script object and then writing out that script to file, the resultant script will still run but won't contain "using terms from" and so won't bug the user.
For getting a reference to the right app, Finder can look for it by ID and simply error if it isn't found rather than bugging the user. You'll need to insert the proper ID's there, I don't know what they are.

Communication between applescript and FileMaker

I'm currently writing an applescript to be run within FileMaker. I need to tell filemaker what the name of its application is (FileMaker Pro or FileMaker Pro Advanced or whatever) so that I can within ANOTHER applescript within filemaker I can say "tell application filemaker"
I currently have a script that figures out the name of the app file, however, the problem is getting it back into filemaker. The name I want to send back is in the applescript variable "FMName" I can think of 2 options
1)
tell application FMName
set cell "gFMName" of current record to FMName
end tell
The problem with this is that even though I know that application FMName will have a set cell command, applescript doesn't and so complains.
2) have the applescript either return a value or an error message and somehow get FileMaker to accept this and do something useful with it.
The problem with this is that 1)it appears that Applescripts can only return numbers (is this true?) 2) I don't want an error dialog or anything, and 3) I have not figured out how to get filemaker to accept this return value in the first place...
Anyone have any better ideas on how to do this?
Thanks!
I know this thread is a bit stale now, but here's what I do - use an if statement. Use the Get(ApplicationVersion) function to find out what kind of FileMaker is running:
If ( PatternCount ( Get ( ApplicationVersion ) ; "Advanced" ) ;
"tell application \"FileMaker Pro Advanced\"" ;
"tell application \"FileMaker Pro\"" )
I have taken this a step further and set it up as a custom function so I don't have it repeated in many different scripts.
Since (if I understand you correctly) no matter what the application is that's being targeted, it's a version of FileMaker, whether FileMaker Pro or FileMaker Pro Advanced or even a runtime version of FileMaker, the dictionary being used will have the same terms available. So, you could use the using terms from application "FileMaker Pro" to enclose the block that you want to execute.
using terms from application "FileMaker Pro"
tell application FMName
set cell "gFMName" of current record to FMName
end tell
end using terms from
I haven't experimented with it, so am not sure if it will work. It seems your goal is to get one FileMaker application to receive data from the AppleScript in another FileMaker application. I do wonder, however, why you're using two FileMaker applications. Are they different versions (i.e., FileMaker 10 and FileMaker 6)? If so, perhaps having the AppleScript write the data to a file that the second FileMaker application then imports is an option. But if they are both FileMaker 7-10, why not simply open the two files in the same application? Then you can write the data from AppleScript directly to the correct file.
This was answered but I though I would add something I discovered lately.
If you target another app, say the Finder, with a "Tell application Finder Activate" then those FMP calls will fail because your now IN the Finder till you do an "End Tell" or explicitly point back to FMP.
The solution that was pointed out to me was to do the out side calls in a single line such as
"Tell application Finder to [what ever you needed the Finder to do]"
This directs just that tell to the non FMP application but leaves you still IN FMP so your FMP calls will work.
This becomes an issue with runtimes since the runtime name will be different from your development application name.

Resources