Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug happens with " and rewrite applescript to javascript #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions applescripts/ApplicationLib.applescript

This file was deleted.

Binary file removed applescripts/ApplicationLib.scpt
Binary file not shown.
142 changes: 0 additions & 142 deletions applescripts/ITunesTransport.applescript

This file was deleted.

Binary file removed applescripts/ITunesTransport.scpt
Binary file not shown.
135 changes: 135 additions & 0 deletions applescripts/iTunesTransport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
function IsRunning () {
ObjC.import('stdlib')
ObjC.import('AppKit')
var isRunning = false
var apps = $.NSWorkspace.sharedWorkspace.runningApplications
apps = ObjC.unwrap(apps)
var app
for (var i = 0, j = apps.length; i < j; i++) {
app = apps[i]
if (typeof app.bundleIdentifier.isEqualToString === 'undefined') {
continue;
}
if (app.bundleIdentifier.isEqualToString('com.apple.iTunes')) {
isRunning = true;
break;
}
}
return isRunning
}

function IsPlaying() {
if (IsRunning()) {
return Application('iTunes').playerState() === 'playing'
}
return false
}

function GetCurrentTrack() {
if (!IsPlaying()) {
return null
}
var iTunes = Application('iTunes')
var track = iTunes.currentTrack
return JSON.stringify({
name: track.name(),
artist: track.artist(),
album: track.album()
})
}

function PausePlaying() {
if (!IsRunning) {
Application('iTunes').pause()
}
return JSON.stringify({ok: true})
}

function StartPlaying() {
var iTunes = Application('iTunes')
iTunes.launch()
iTunes.play()
return GetCurrentTrack()
}

function StopPlaying() {
if (IsRunning()) {
Application('iTunes').stop()
}
return JSON.stringify({ok: true})
}

function PlayNextTrack() {
var iTunes = Application('iTunes')
if (!IsRunning()) {
iTunes.activate()
}
iTunes.nextTrack()
StartPlaying()
}

function PlayPreviousTrack() {
var iTunes = Application('iTunes')
if (!IsRunning()) {
iTunes.activate()
}
iTunes.previousTrack()
StartPlaying()
}

function FadeOut () {
if (IsRunning() && IsPlaying()) {
var iTunes = Application('iTunes')
var originalVol = iTunes.soundVolume()
var currentVol = iTunes.soundVolume()
while(currentVol > 0) {
currentVol--
iTunes.soundVolume = currentVol
delay(0.02)
}
StopPlaying()
iTunes.soundVolume = originalVol
}
}

function FadeIn () {
var iTunes = Application('iTunes')
if (!IsRunning()) {
iTunes.activate()
}
if (!IsPlaying()) {
var originalVol = iTunes.soundVolume()
var currentVol = 0
iTunes.soundVolume = currentVol
iTunes.play()
while(currentVol < originalVol) {
currentVol++
iTunes.soundVolume = currentVol
delay(0.02)
}
return GetCurrentTrack()
}
}

function run(argv) {
var command = argv[0]
if (command === "currenttrack") {
return GetCurrentTrack()
} else if( command === "play") {
return StartPlaying()
} else if( command === "pause") {
return PausePlaying()
} else if( command === "stop") {
return StopPlaying()
} else if( command === "next") {
return PlayNextTrack()
} else if( command === "previous") {
return PlayPreviousTrack()
} else if( command === "fadeout") {
return FadeOut()
} else if( command === "fadein") {
return FadeIn()
} else {
return JSON.stringify({error: 'this command is not supported'})
}
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var Playback = function() {
this.runTransportScript = function(command, callback) {
var scriptPath = this.isWindows ?
path.join(__dirname, 'windows_scripts', 'iTunes.js') :
path.join(__dirname, 'applescripts', 'ITunesTransport.scpt');
path.join(__dirname, 'applescripts', 'ITunesTransport.js');
var scriptRunner = this.isWindows ?
spawn('cscript', ['//Nologo', scriptPath, command]) :
spawn('osascript', [scriptPath, command]);
Expand Down