Applescript to launch iTunes with a specific library - macos

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.

Related

How to open an EMACS file in OS X by double-clicking on it, using the `emacsclient` command?

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.

Opening a file with an app using a command line in apple script or an automator app

I want to be able to double click to open a files type (HTML) in a particular app (SeaMonkey composer).
Double clicking the file opens SeaMonkey Browser, but I want it to open in Seamonkey Composer. The only way to do it is with the following command line
seamonkey -editor "filename.html"
So, how can I use apple script or automator to open my html files in composer ?
Save the following script as Application in Script Editor:
on run filesList
repeat with fileRef in filesList
do shell script "seamonkey -editor " & quoted form of POSIX path of fileRef
end repeat
end run
Select View > Show Bundle Contents and give it a custom bundle ID. You can then change the file association as above.
The above assumes the seamonkey command is itself just a launcher; if it's actually the full application (which may be the case as it's obviously not a native Mac app), the middle line'll need tweaked a bit:
do shell script "nohup seamonkey -editor " & quoted form of POSIX path of fileRef & " >/dev/null 2>&1"
That should allow the shell script to exit as soon as the seamonkey process is launched, leaving Seamonkey running until you quit it from its GUI.
If you want to use AppleScript to do this, this simple script should do the job:
set filePath to ((path to documents folder) as text) & "filename.html"
tell application "Seamonkey Composer" to open filePath
I don't have the Seamonkey Composer app to test, but it works with BBEdit.
Note that the open command must have a full path to the file.

How to debug droplets using AppleScript

Just 3 lines of script to be able to test a droplet application without leaving applescript editor
set fich to POSIX file "/Appli/conv2spct.app" as string
tell application "Finder" to open POSIX file "/Users/yourusername/Desktop/somefile" using application file fich
If there are errors in your droplet a display dialog will be opened by the script editor applescript
The same script with choose file for the 2 elements
set fileappli to POSIX path choose file of type {"APPL"} with prompt "Choose a Droplet application to debug"--the droplet for debug
set fileargument to POSIX path choose file --the file argument to pass at droplet
tell application "Finder" to open fileargument using application file fileappli
If there are errors in your droplet a display dialog will be opened by the script editor applescript
Here's a pragmatic alternative using do shell script, which potentially allows you to specify multiple file arguments:
do shell script "open -a /Appli/conv2spct.app ~/Desktop/somefile1 ~/Desktop/somefile2"
The above paths happen not to need quoting (escaping) for the shell, but when using variables to specify the file paths, it's best to use quoted form of (to pass multiple arguments, apply quoted form of to each):
do shell script "open -a " & quoted form of fileappli & " " & quoted form of fileargument
Looks like this has become simpler since the question was first asked. According to this documentation you can write:
open {choose file}
on open theDroppedItems
...
end open
Run this from within the AppleScript editor and the file you choose will be opened as if it had been dropped onto the compiled script.

Script that launches commands on two Terminals

I need to do a .command file script/batch. Launching it (double-click) it has to to those things:
Open a terminal window (A)
Launching a command that open the folder where the file is (maybe this cd "dirname "$0"")
Launch a command
Open a terminal window (B)
Launching same command at point 2
Launch a command
Given that you explicitly want to create terminal windows, consider creating an application using AppleScript:
Open Script Editor (up to 10.9, AppleScript Editor)
Paste the code below.
Save as an application (via the pop-up list in the Save As dialog) to the desired folder.
# Determine the folder in which this app is located.
set thisFolder to do shell script "dirname " & quoted form of POSIX path of (path to me)
# Sample commands to execute in the new windows.
set cmds to {"date", "echo $$"}
tell application "Terminal"
# Create 2 new windows, change to the
# this app's folder, and execute the respective command.
repeat with i from 1 to 2
do script "cd " & quoted form of thisFolder & "; " & item i of cmds
end repeat
# Activate Terminal.app
activate
end tell
The reason that I recommend using an application over a *.command file is that the latter would itself open in a Terminal window first, before creating the desired windows, which is visually disruptive (and, depending on your Terminal.app preferences, may leave the extra window open).
Alternatively, you could turn that into a virtue and use the *.command file's own window as your 1st terminal window, and only create one additional one.

Is there a way to trigger Finder's "quick look" window with Applescript?

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.

Resources