Tell application process that contain a word - macos

This question may seem strange. However I'm in the need of interacting with an application that is releasing worldwide with different names, eg. AppEN, AppGB, AppDE etc...
I am looking for a solution that allow me to use this command:
tell application process "AppnameGB"
However it should work with all the different variations of this application. I don't know if this is possible but searching for a string in the application name could do the trick: tell application process that contain in its name "Appname".

If the process is already open, you can use something like this:
tell application "System Events"
tell (process 1 where name starts with "TextEd")
properties
set f to its file
end tell
end tell
tell application (f as text)
properties
end tell
Telling Finder to list files is really slow:
tell application "Finder"
item 1 of (path to applications folder) where name starts with "Text"
end tell
You can use do shell script though:
set a to do shell script "ls /Applications/ | grep -m1 '^Text.*\\.app$'"
tell application a
properties
end tell
set a to do shell script "mdfind 'kMDItemContentType==com.apple.application-bundle&&kMDItemFSName==Text*' | head -n1" would also search outside the applications folder.

The easiest is if you could use the bundle identifier of the application instead of its name. Most likely the bundle ID doesn't change while the name does. So use this script on that application and check if the bundle ID changes or not.
tell application "Finder" to set bundleID to id of (choose file)
If you find it doesn't change then you can access it via applescript like this...
tell application "com.bundle.id"
activate
-- do something
end tell
Your only other alternative is get a list of all applications, and loop through them checking the name as you suggest. Something like this wold work but is pretty slow.
-- use spotlight to find all the apps on the computer
set cmd to "mdfind 'kMDItemContentType == \"com.apple.application-bundle\"'"
set foundApps to paragraphs of (do shell script cmd)
-- search the found apps looking for the one you're interested in
set appPath to missing value
repeat with i from 1 to count of foundApps
if (item i of foundApps) contains "Appname" then
set appPath to item i of foundApps
exit repeat
end if
end repeat
if appPath is missing value then error "Couldn't find the app!"
-- open the app
set macAppPath to (POSIX file appPath) as text
tell application macAppPath
activate
-- do something
end tell

Related

Get the name of the parent directory of an applescript file

I'm making a small script that needs to use the name of the parent directory of the script itself (.scpt file) a variable.
For example, the script is located at /Users/spongebob/MyProject/myscript.scpt
I need to set the variable called myprojectdir to MyProject.
I've tried
set myprojectdir to parent of POSIX path of me
and other variations of this based on search results but I always end up with an error
Can’t get POSIX path.
Where am I going wrong?
Thanks
AppleScript itself has no idea.
You have to ask System Events
tell application "System Events" to set myprojectdir to name of container of (path to me)
or the Finder
tell application "Finder" to set myprojectdir to name of parent of (path to me)

Reveal in finder does not work with concatenated string

Why does this work:
tell application "Finder"
activate
reveal POSIX file ("/Users/Torben/Library/Mobile Documents/com~apple~CloudDocs/MyFolder/file.png")
end tell
...but not this
tell application "Finder"
activate
reveal POSIX file ("/Users/Torben/Library/Mobile Documents/com~apple~CloudDocs/MyFolder/" & "file.png")
end tell
And how do I get it to work if I want to join a path (string) with a variable (string)?
System Events handles POSIX paths a lot better, but this is just another one of those AppleScript oddities. POSIX file will work outside of a Finder tell statement:
set x to POSIX file (pathVariable & otherPathVariable)
tell application "Finder"
activate
reveal x
end tell
but within a Finder tell statement you need to use it as a coercion:
tell application "Finder"
activate
reveal (pathVariable & otherPathVariable) as POSIX file
end tell
I recommend to use relative HFS paths. The first line points to the library folder of the current user.
set libraryFolder to path to library folder from user domain as text
tell application "Finder"
reveal file (libraryFolder & "com~apple~CloudDocs:MyFolder:" & "file.png")
end tell
try this to combine strings
reveal POSIX file (("/Users/Torben/Library/Mobile Documents/com~apple~CloudDocs/MyFolder/" & "file.png") as text)

Relative path to AppleScript exported as .app

I have an AppleScript exported as a .app file because I need it to run on log in.
This is my code:
repeat
tell application "System Events"
repeat with desktopNumber from 1 to count of desktops
tell desktop desktopNumber
set picture to "~/Desktop/script/img.jpg"
end tell
end repeat
end tell
end repeat
The path to the script is ~/Desktop/script/script.app/Contents/Resources/Scripts/main.scpt
I'd like to put the image in the Resources folder as well and make the path relative so i can put the folder anywhere without changing my script so I tried
set desktopPicture to ((container of container of (path to me)) as text) & "/img.jpg"
repeat
tell application "System Events"
repeat with desktopNumber from 1 to count of desktops
tell desktop desktopNumber
set picture to desktopPicture
end tell
end repeat
end tell
end repeat
But that gives me the error Can’t make container of container of alias \"Macintosh HD:Users:Me:Desktop:script:script.app:Contents:Resources:Scripts:main.scpt\" into type text.
System Events is not able to expand the tilde ~.
If you want to refer to ~/Desktop/script/img.jpg using relative paths you could use
tell application "System Events" to set desktopPicture to file "img.jpg" of folder "script" of desktop folder
or
set desktopPicture to alias ((path to desktop folder as text) & "script:img.jpg")
Consider that in both cases AppleScript will throw an error if the file does not exist.

How to change Current Working directory using AppleScript

What would be a simplest way to change a current working directory (cwd) using AppleScript. It would be equivalent to OS's cd or Python's os.chdir. Would be great if a destination directory if doesn't exist would be created on a fly (but that would be highly optional).
If you are looking to use with an applications save dialog...
set filePath to "path/to/my/file"
tell application "System Events"
-- Once save dialog is open
keystroke "g" using {shift down, command down}
delay 1
set value of text field 1 of sheet 1 of sheet 1 of window 1 to filePath
end tell
You might be thinking of doing something like this:
tell application "Finder"
# ^^^ or whatever application you want to control up there
# to get to a file...
set filePath to POSIX file "/Users/username/Documents/new.mp3"
# to get to a folder...
set testFolder to POSIX path "/Users/username/test"
if (exists (folder testFolder)) then
say "folder exists"
else
make new folder at testFolder
endif
end tell
I'm basing my answer off the answers on this page and this related question (or this one).

OS X 10.8.4: Applescript giving syntax error for 'property list file'

I want to find whether or not a specific version of Firefox was installed on a Mac that may have one or more different versions of Firefox installed. I can't simply use version of application "Firefox" because I may miss alternate installations of Firefox that way. My Applescript code so far is:
tell application "Finder"
set firefox_list to paragraphs of (do shell script "find /Applications -name Firefox.app")
repeat with ff_installed in firefox_list
-- get version
set ff_ins_info to property list file (POSIX path of ff_installed & "/Contents/Info.plist")
set ff_ins_version to (value of property list item "Bundle version" of ff_ins_info)
display dialog ff_ins_version
end repeat
end tell
Unfortunately I get an error message when I try to compile: Syntax Error: Expected expression but found “property”. The best I can make out is that Applescript doesn't seem to recognize property list file as an object. As I understand it, this should work after 10.5.
I've considered writing a shell script to look up the version number using Apple's PlistBuddy utility, but that seems even more complicated than this approach. Am I left with greping through Plist XML directly at this point?
Thanks in advance for any suggestions!
I made 3 changes to get it working:
replaced tell application "Finder" with tell application "System Events"
left out POSIX path of
replaced Bundle version with CFBundleVersion
This is the final version
tell application "System Events"
set firefox_list to paragraphs of (do shell script "find /Applications -name Firefox.app")
repeat with ff_installed in firefox_list
-- get version
set ff_ins_info to property list file (ff_installed & "/Contents/Info.plist")
set ff_ins_version to (value of property list item "CFBundleVersion" of ff_ins_info)
display dialog ff_ins_version
end repeat
end tell

Resources