Showing posts with label AppleScript. Show all posts
Showing posts with label AppleScript. Show all posts

Tuesday, September 27, 2016

AppleScript: iTunes: Get All the Artwork of Current Playlist

on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars

tell application "iTunes"

set trackArray to tracks of current playlist

set lastAlbum to null
repeat with aTrack in trackArray
set currentAlbum to album of aTrack as string
if currentAlbum is not lastAlbum then
if exists every artwork of aTrack then
set srcBytes to raw data of artwork 1 of aTrack
if format of artwork 1 of aTrack is «class PNG » then
set ext to ".png"
set ext to ".jpg"
end if

set str to my replace_chars(currentAlbum, ":", "-")

set fileName to (((path to desktop) as text) & str & ext)

-- write to file
set outFile to open for access file fileName with write permission
-- truncate the file
set eof outFile to 0
-- write the image bytes to the file
write srcBytes to outFile
close access outFile
end if
end if
set lastAlbum to currentAlbum
end repeat
end tell

Monday, March 21, 2016

AppleScript: Opening iCloud Documents (Numbers)

set the defaultDestinationFolder to (path to desktop folder)
set filePath to (path to library folder from user domain as text) & "Mobile Documents:com~apple~Numbers:Documents:test.numbers"

tell application "Numbers"
        open file filePath
    end try
end tell

Tuesday, January 19, 2016

AppleScript: iTunes: Creating a playlist with songs with lyrics

set outputPlaylistName to "Songs with lyrics"
set maxTrackCount to 100000

-- Dialog Text input
display dialog "Enter the playlist name you want to search from" default answer ""
set inputPlaylist to text returned of result

-- Check input text
if inputPlaylist is not "" then

tell application "iTunes"

-- Playlist name present?
if (not (exists playlist inputPlaylist)) then
display dialog "No such playlist"
end if

-- Check number of result
set trackCount to count of tracks of playlist inputPlaylist

-- If number of tracks are over max
if (trackCount ≥ maxTrackCount) then
display dialog "Too many files in the playlist: " & trackCount
end if

-- Does the output playlist name exists?
if (exists user playlist outputPlaylistName) then
delete tracks of user playlist outputPlaylistName
end try
make new user playlist with properties {name:outputPlaylistName}
end if

-- Iterate through the input playlist
set resultTracks to every file track of playlist inputPlaylist
repeat with aTrack in resultTracks
if ((location of aTrack as string) is not equal to "missing value") then
-- if lyrics exists in the input track
if (not (lyrics of aTrack is equal to "")) then
-- Add to the output playlist
duplicate aTrack to user playlist outputPlaylistName
on error
log ("unknown error: " & (artist of aTrack as string) & " - " & (name of aTrack as string))
end try
end if
log (location of aTrack as string)
log ("file couldn't found: " & (artist of aTrack as string) & " - " & (name of aTrack as string))
end if
end repeat
end tell
end if

Thursday, January 14, 2016

AppleScript: Dialog box

Code 1:
display dialog "Dialog" buttons {"A", "B"} default button "B" giving up after 3

Code 2:
display dialog "The Continuation Character test." buttons {"A", "B"} ¬
default button "B" giving up after 3