I'm working on adding an app icon to the Dock programmatically. But all the information I found indicates I need to restart the Dock after the change.
Is there any possible method that can add icon to the Dock "live" that doesn't need to kill the Dock process? Can I perhaps use Mach IPC?
I found the following AppleScript. It worked on my system.
my add_item_to_dock(choose file of type {"APPL"} with prompt "Choose an application to add to the Dock:")
on add_item_to_dock(item_path)
try
get item_path as alias -- you need a POSIX path so this coerces the path in case it's an HFS path, alias, file ref, etc.
set item_path to POSIX path of item_path
end try
try
tell application "Dock" to quit
end try
do shell script "defaults write com.apple.dock persistent-apps -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & item_path & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
try
tell application "Dock" to activate
end try
end add_item_to_dock
This entry will be stored in ~/Library/Preference/com.apple.dock.plist
Hope this will be helpful.
Related
I want to open an org-mode file selected in the Finder, by double clicking on it. But since I use Emacs in daemon-mode, I want to use the emacsclient command for that.
Thus the primary idea was to wrap the command emacsclient -c posixPathToFile in an AppleScript App to open it.
tell application "Finder"
set fileAlias to the selection as alias
set fileName to name of fileAlias
set posixPath to POSIX path of fileAlias
end tell
-- tell application "Emacs" to activate
try
do shell script "/usr/local/bin/emacsclient -c " & quoted form of posixPath
end try
I know some set commands are not needed. Let's assume this script is saved as Xemacs.app and that I associate this app to always open .org file.
Using this App does not work by double-clicking on the file, but rather if I select the file in the Finder and then call the Xemacs.app independently. Why ? I'm not confident enough with AppleScript to figure out what happens.
So the workaround was to use the Automator service
on run {input, parameters}
set posixPath to POSIX path of input
tell application "iTerm" to do shell script "/usr/local/bin/emacsclient -c " & quoted form of posixPath
return input
end run
The service is saved as 'Open in Emacs'
Now selecting a file and right-clicking and callig Service > "Open in Emacs" works and opens the file.
What is wrong with the first approach ?
ok, I solved my issue. The problem comes from my misunderstanding of the difference between ScriptEditor and the Automator. If I use the Automator to create an App and use the former script instead of creating an App using the ScriptEditor, then it works as expected.
One can simplify the process by creating an App in Automator and running a shell script instead of wrapping the command in Ascript.
I would like my application to be automatically added to the dock after the package (DMG) is installed?
Does anyone know how to do this?
There's a couple ways to do this, Andrew, and a lot of this depends on how you're doing your application installing.
If you are using PackageMaker to install your app, you can run a "postflight" script which adds your app's icon to the "defaults" (i.e. the preferences) of the dock. This older MacRumors thread shows how to do that.
If you are not using PackageMaker, then you might have to run an Applescript from within your app that does the same "defaults" writing trick. Here's a thread on AskDifferent that shows how.
In both cases you need to kill the dock (or Finder?) and restart it in order to get the change to pick up and show.
I would suggest you run the following AppleScript code, replacing myapp with the app you want to add to de dock, including its path.
In the example below, I am adding the system app "System Preferences", but you can do the same with your own path, just assign the path of your app to the myapp variable.
on run
set myapp to "/Applications/System Preferences.app"
try
tell application "Dock" to quit
end try
do shell script "defaults write com.apple.dock persistent-apps -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & myapp & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
try
tell application "Dock" to activate
end try
end run
This follows the suggestion given by Michael, but it should be more gentle as it just quits the Dock rather thank killing it.
If you prefer bash, you could run the following code, again assigning the path of your own app to the myapp variable.
Note: in the bash case, you have to use double slashes when specifying your the path, as you can see in the example.
#!/bin/bash
myapp="//Applications//System Preferences.app"
defaults write com.apple.dock persistent-apps -array-add "<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>$myapp</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>"
osascript -e 'tell application "Dock" to quit'
osascript -e 'tell application "Dock" to activate'
I am trying to use Remote Buddy to control Photo Booth, but I need a way to switch between Still and Video modes, my solution to this was to use an Automator app to select one or other of the two radio buttons when a remote button is pressed.
I've created the .app, and it works fine when I double click it from the desktop, but I need a way to launch the .app from within Remote Buddy, and AppleScript seems to be my only option.
tl;dr
I need to be able to launch an Automator .app file using AppleScript, but can't figure out the correct syntax.
If I made an Automator app named Untitled I would start it by using this command tell application "Untitled" to activate
Once you create an application using one of the following methods, that application can be accessed in any other script via its name. It's defined globally, as is any other application on your mac. Just use the tell application "app Name"
Two ways of creating an application :
activate app ((system attribute "HOME") & "/Desktop/test.app/")
You could also use the automator shell command.
automator test.workflow
automator test.app
automator test.workflow -v # verbose
automator -i lol test.workflow
echo lol | automator -i - test.workflow
automator -i $'lol\nlol2' test.workflow # \n separates input strings
automator -d somevar=somevalue test.workflow
You would first name your automator app for example "photobooth.app" then you would go in applescript an type in
tell application "photobooth.app"
activate
end tell
I do it directly with Automator scripting. This does not access the app, but rather the workflow. It is advantageous, because you can edit the settings/contents of some of the individual workflow items.
I suppose my answer would be better suited for the question:
How can I launch an Automator Workflows using AppleScript?
I find that saving the Automator action first avoids problems. e.g.
set theWorkflowName to "Merge PDF Files"
set myWorkflow to make new workflow with properties {name:theWorkflowName, path:POSIX path of ((path to temporary items as string) & theWorkflowName & ".workflow" as string)}
set myWorkflow to open POSIX path of ((path to temporary items as string) & theWorkflowName & ".workflow" as string)
Merge PDF Files Droplet
on open the_Droppings
-- CONVERT INPUT LIST OF ALIASES TO POSIX PATHS
repeat with itemStep from 1 to count of the_Droppings
set item itemStep of the_Droppings to POSIX path of item itemStep of the_Droppings
end repeat
tell application "Automator"
activate
set myWorkflow to open POSIX file "/Users/USERNAME/Dropbox/Scripts/Automator/Workflows/merge PDF files.workflow"
set actionsList to name of Automator action of myWorkflow
set firstAction to item 1 of actionsList
tell myWorkflow
(*
get index of Automator action firstAction
get input types of Automator action firstAction
get path of Automator action firstAction
get path of Automator action firstAction
get value of setting of Automator action firstAction
*)
set value of setting of Automator action firstAction to the_Droppings -- MUST BE LIST OF POSIX PATHS
end tell
end tell
end open
I am using Applescript to automate some tasks in the OSX Finder. The script opens up a folder and selects the first image in that folder. I would like it to also bring up the "quick look" window (exactly as if the user had pressed the space bar).
I did find a way to fire up quick look from the command line using qlmanage, but that brings up a static quick look window, which is no longer tied to the finder selection.
Code so far:
property folderPath : "/Volumes/Media/Images"
on run {}
tell application "Finder"
activate
set imageFolder to folder (folderPath as POSIX file)
set imageFile to first item of imageFolder
select imageFile
-- show quick look?
end tell
end run
If you don't want to do it by scripting the Finder you can run the following shell command
qlmanage -p thefile
In an Applescript you might do this like
do shell script "qlmanage -p " & "thepath/thefile"
Depending upon what you are doing this might be much easier. Especially if you primarily just have a set of paths.
If you have an existing Applescript path you can send it like this
set p to POSIX path of mypath
do shell script "qlmanage -pr " & quoted form of p
Updated (with thanks to Kevin Ballard):
tell application "System Events" to keystroke "y" using command down
Note: this requires that "enable access for assistive devices" is selected in the "Universal Access" control panel.
I would like to write an AppleScript that would allow me to launch iTunes with a given Library instead of having to hold down the Option key and browsing for one. I'm already aware of Doug's Library manager, which is not quite what I want. The AppleScript would be for a specific library.
iTunes doesn't allow you to do this with AppleScript, but you can write directly into iTunes' preferences, where it stores a bookmark (alias) to the currently selected library (or nothing, if you're using a library in the default location).
First, you'll need to obtain the alias data for your selected library location. Open iTunes holding down the Option key, select your library and quit iTunes. Then, in Terminal, run:
defaults read com.apple.itunes 'book:1:iTunes Library Location' | pbcopy
This will copy the library alias data to the clipboard.
Finally, here's the script:
property otherLibraryLocation : "" -- paste location between the quotes
property libraryLocationPref : "com.apple.iTunes 'book:1:iTunes Library Location'"
-- first, quit iTunes if it's running
tell application "System Events"
if exists (application process "iTunes") then
tell application "iTunes" to quit
end if
end tell
-- then, set the location
do shell script "defaults write " & libraryLocationPref & " " & quoted form of otherLibraryLocation
-- uncomment the following line to use the default iTunes library instead
-- do shell script "defaults delete " & libraryLocationPref
-- finally, relaunch iTunes
tell application "iTunes" to activate
Paste the library location between the quotes in the first line of the script, and you should be all set. To return to the original library, uncomment the line including defaults delete.
You can create a symlink from ~/Music/iTunes to your chosen directory path in a unix shell script (man ln). And an AppleScript can call a unix shell script by sending the appropriate message to the Terminal app.