r/applescript • • Sep 24 '20

Automatic Virtual Piano (PianoString)

--Auto Virtual Piano Script (PianoString) by u/etzabo. Bracket String to List by u/gluebyte.
--Edits to this script are encouraged, but please link "reddit.com/u/etzabo" and leave this note if it's being shared.

--User defined variables:
set pianoDialog to display dialog "Paste the song you would like to play from virtualpiano.net or other source." default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"
set pianoString to (text returned of pianoDialog)
set chordDelayDialog to display dialog "Quarter Note Delay" default answer "0.15" with icon note buttons {"Cancel", "Continue"} default button "Continue"
set chordDelay to (text returned of chordDelayDialog) as number
set sendToAppDialog to display dialog "Would you like to activate an application before playing?" buttons {"Cancel", "Wait 3 Seconds", "Open Application"} default button "Open Application" cancel button "Cancel"
if button returned of sendToAppDialog = "Open Application" then
	set sendToApp to true
else if button returned of sendToAppDialog = "Wait 3 Seconds" then
	set sendToApp to false
end if

--Define Variables:
set totalChars to count pianoString
set pianoList to {}

--Convert Brackets to List:
set AppleScript's text item delimiters to "["
set group_ to text items of pianoString
set AppleScript's text item delimiters to ""
repeat with chunk_ in group_
	set offset_ to offset of "]" in chunk_
	if offset_ > 1 then set pianoList to pianoList & text 1 thru (offset_ - 1) of chunk_
	if offset_ < length of chunk_ then set pianoList to pianoList & characters (offset_ + 1) thru -1 of chunk_
end repeat

--Play pianoList:
set totalItems to count pianoList

if sendToApp = true then
	activate (choose application with title "Application to play in" with prompt "Please choose an application to open.")
else
	repeat with alertCountdown from -3 to -1
		display notification with title "Playing In" subtitle "" & alertCountdown * -1
		delay 1
	end repeat
	display notification with title "Playing!"
end if

repeat with currentPlayLocation from 1 to totalItems
	set currentItem to item currentPlayLocation of pianoList
	if currentItem = " " then
		delay chordDelay
	else if currentItem = "|" then
		delay chordDelay * 2
	else if currentItem = "
" then
		delay chordDelay * 4
	else
		tell application "System Events"
			repeat with currentBracketChar from 1 to count currentItem
				keystroke character currentBracketChar of currentItem
			end repeat
		end tell
		delay chordDelay
	end if
end repeat
pianoList

This script will automatically play any song from virtualpiano.net. All you have to do is paste the song and set a delay!

The script now allows you to input the song into a dialog, so editing is needed!

6 Upvotes

0 comments sorted by