Last active
December 26, 2018 15:03
-
-
Save xreiju/9f76d81d9966eb0d440874909bd17927 to your computer and use it in GitHub Desktop.
osascript -l JavaScript nowplaying.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const iTunes = Application('iTunes') | |
const app = Application.currentApplication() | |
app.includeStandardAdditions = true | |
const hookURL = 'https://misskey-hook.firebaseapp.comで取得して' | |
function getTrack(iTunes) { | |
return iTunes.currentTrack() | |
} | |
function getTrackData(iTunes) { | |
let track = getTrack(iTunes) | |
let { name, album, artist, time } = track | |
let data = { | |
name, | |
album, | |
artist, | |
time | |
} | |
for (let key in data) { | |
data[key] = data[key]() | |
} | |
return data | |
} | |
function getPlayerState(iTunes) { | |
const status = { | |
'stopped': '⏹️ Stopped.', | |
'playing': '▶️ Playing.', | |
'paused': '⏸ Paused.', | |
'fast forwarding': '⏩ Fast Forwarding.', | |
'rewinding': '⏪ Rewinding.' | |
} | |
return status[iTunes.playerState()] | |
} | |
function getCurrentTime(iTunes) { | |
let time = iTunes.playerPosition() | |
time = Math.ceil(time) | |
let minute = Math.floor(time / 60) | |
let second = time % 60 | |
return `${minute}:${('00' + second).substr(-2)}` | |
} | |
const host = 'misskey.xyz' | |
let data = getTrackData(iTunes) | |
let text = `🎶 ${data.name} by 🎤 ${data.artist}\n💿 ${data.album}\n⏰ ${getCurrentTime(iTunes)} / ${data.time} ${getPlayerState(iTunes)}\n#nowplaying #reiju-nowplaying #iTunes` | |
function escapeStr(str) { | |
return str | |
//.replace(/\\/g, '\\\\') | |
//replace(/'/g, "\\'") | |
.replace(/"/g, '\\"') | |
//.replace(/</g, '\\x3c') | |
//.replace(/>/g, '\\x3e') | |
//.replace(/(0x0D)/g, '\r') | |
//.replace(/(0x0A)/g, '\n') | |
} | |
function sendPost(text, hookURL) { | |
let command = `echo "${escapeStr(JSON.stringify(Object({text})))}" | curl -H "Content-Type: application/json" -d @- "${hookURL}"` | |
let res = app.doShellScript(command) | |
return res | |
} | |
async function playSuccessSound() { | |
await app.doShellScript('afplay "/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/burn\ complete.aif"') | |
} | |
let res = sendPost(text, hookURL) | |
let resJson | |
try { | |
resJson = JSON.parse(res) | |
playSuccessSound() | |
let showText = `Posted as ${resJson.createdNote.user.name}(@${resJson.createdNote.user.username}\n${text})` | |
} catch(e) { | |
app.displayAlert('An error occured while parsing the response') | |
app.displayAlert(res) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment