This script turns a selection of text in Safari and turn it to a audiobook in iTunes, optionally sync it to your iphone/ipod.
-- -*- mode: applescript -*- --
-- My first applescript:
-- This script makes a text selection to speech and import to iTunes
-- much of it is a total rip off from other scripts you see online,
-- especially those from http://dougscripts.com/. Thanks Doug!
--
-- 13z73rch3un9 [at] gmail.com
-- configurables -------------
set tmpDir to (path to desktop folder as text)
set plName to "reading materials"
set myIpodName to "NAME OF YOUR IPHONE/IPOD"
---------------------------------
tell application "Safari"
set selecTxt to (do JavaScript "(getSelection())" in document 1)
-- set pgtitle to quoted form of (do JavaScript "document.title" in document 1)
set pgtitle to (do JavaScript "document.title" in document 1)
set pgurl to (do JavaScript "document.URL" in document 1)
end tell
set pgtitle to replace(pgtitle, ":", " -")
set aiffFile to tmpDir & pgtitle & ".aiff"
say selecTxt saving to aiffFile
-- import to iTunes and add ID3 tags
tell application "iTunes"
set rawTrack to (add aiffFile)
tell application "Finder" to delete aiffFile
-- metadata
set name of rawTrack to pgtitle
set ds to ((current date) as string)
set comment of rawTrack to "Imported from Safari on " & ds & return & "source: " & pgurl
set genre of rawTrack to "reading material"
set lyrics of rawTrack to selecTxt
-- I don't know why this doesn't work....
-- set y to ((year of (current date)) as string)
-- set year of myTrack to y
set year of rawTrack to (do shell script "/bin/date +%Y")
-- transcode
set defaultEncoder to current encoder
set aacEncoder to (item 1 of (every encoder whose format is "AAC"))
set current encoder to aacEncoder
set m4aTrack to item 1 of (convert rawTrack)
set current encoder to defaultEncoder
delete rawTrack
set m4aLocation to location of m4aTrack
tell application "Finder"
set m4aAlias to m4aLocation as alias
set file type of m4aAlias to "M4B "
if name extension of m4aAlias is not "m4b" then
set name of m4aAlias to ((text 1 thru -((length of (get name extension of m4aAlias)) + 1) of (name of m4aAlias as text)) & "m4b") as string
end if
end tell
delete m4aTrack
set m4bTrack to add m4aAlias
reveal m4bTrack
-- uncomment this to sync ipod/iphone connected, change your device name!
try
update (some source whose name is myIpodName)
on error
display alert "Your iPod is not connected, not syncing... (Have you defined your ipod name in the script?)"
end try
end tell
tell application "Safari" to activate
on replace(str, sstr, newstr)
set rtn to ""
repeat with i from 1 to (length of text of str) by 1
set c to text i of str
if c is equal to sstr then
set rtn to rtn & newstr
else
set rtn to rtn & c
end if
end repeat
return rtn
end replace
-- My first applescript:
-- This script makes a text selection to speech and import to iTunes
-- much of it is a total rip off from other scripts you see online,
-- especially those from http://dougscripts.com/. Thanks Doug!
--
-- 13z73rch3un9 [at] gmail.com
-- configurables -------------
set tmpDir to (path to desktop folder as text)
set plName to "reading materials"
set myIpodName to "NAME OF YOUR IPHONE/IPOD"
---------------------------------
tell application "Safari"
set selecTxt to (do JavaScript "(getSelection())" in document 1)
-- set pgtitle to quoted form of (do JavaScript "document.title" in document 1)
set pgtitle to (do JavaScript "document.title" in document 1)
set pgurl to (do JavaScript "document.URL" in document 1)
end tell
set pgtitle to replace(pgtitle, ":", " -")
set aiffFile to tmpDir & pgtitle & ".aiff"
say selecTxt saving to aiffFile
-- import to iTunes and add ID3 tags
tell application "iTunes"
set rawTrack to (add aiffFile)
tell application "Finder" to delete aiffFile
-- metadata
set name of rawTrack to pgtitle
set ds to ((current date) as string)
set comment of rawTrack to "Imported from Safari on " & ds & return & "source: " & pgurl
set genre of rawTrack to "reading material"
set lyrics of rawTrack to selecTxt
-- I don't know why this doesn't work....
-- set y to ((year of (current date)) as string)
-- set year of myTrack to y
set year of rawTrack to (do shell script "/bin/date +%Y")
-- transcode
set defaultEncoder to current encoder
set aacEncoder to (item 1 of (every encoder whose format is "AAC"))
set current encoder to aacEncoder
set m4aTrack to item 1 of (convert rawTrack)
set current encoder to defaultEncoder
delete rawTrack
set m4aLocation to location of m4aTrack
tell application "Finder"
set m4aAlias to m4aLocation as alias
set file type of m4aAlias to "M4B "
if name extension of m4aAlias is not "m4b" then
set name of m4aAlias to ((text 1 thru -((length of (get name extension of m4aAlias)) + 1) of (name of m4aAlias as text)) & "m4b") as string
end if
end tell
delete m4aTrack
set m4bTrack to add m4aAlias
reveal m4bTrack
-- uncomment this to sync ipod/iphone connected, change your device name!
try
update (some source whose name is myIpodName)
on error
display alert "Your iPod is not connected, not syncing... (Have you defined your ipod name in the script?)"
end try
end tell
tell application "Safari" to activate
on replace(str, sstr, newstr)
set rtn to ""
repeat with i from 1 to (length of text of str) by 1
set c to text i of str
if c is equal to sstr then
set rtn to rtn & newstr
else
set rtn to rtn & c
end if
end repeat
return rtn
end replace
0 comments:
Post a Comment