I want to add song files to iTunes via the command line.
open -a iTunes -g song.mp3
does exactly this. - However it starts playing the song, too. Any way to avoid this?
You can use the "Automatically Add to iTunes" folder for that (see PH19617):
cp song.mp3 ~/"Music/iTunes/iTunes Media/Automatically Add to iTunes/"
If iTunes is running, anything you copy into this folder will be imported into iTunes automatically (and deleted from the folder afterwards).
You can use applescript to do so and there's much more to explore. (add music to special playlist, etc.)
for example, you need to add file /User/username/myMusic/my.mp3 to lib, create a script file named add.scpt
tell application "iTunes" add alias ((path to home folder as string) & "myMusic:my.mp3") end tell