0

Sorry in advance if this question has been answered, but I honestly don't even know what to google since there's not much info on reading/writing to ANY database using AppleScripts (at least none that I can get working or make sense...)

Background: I'm trying to use AppleScripts to manage my playlists in Apple Music.

I have the structure of my music library as follows: (bold are folders) -Library:
--Database
---Artists
----Adele
-----Discography
-----AppleEssentials
-----Unreleased
-----EPs
-----Remixes
-----Featured in (like ft. Adele)
----Atmosphere
-----Discography
-----AppleEssentials
-----Unreleased
-----EPs
-----Remixes
-----Featured in
...
...
...
----X Ambassadors
-----Discography
-----AppleEssentials
-----Unreleased
-----EPs
-----Remixes
-----Featured in
---Tags
----Song Tags
-----Happy
-----Sad
...
-----Upbeat
----Person Tags
----Reference Tags
...
and you get the idea.

Since Apple Music doesn't have a good tagging system, I created a shortcut in iOS that allows me to add the current song to multiple playlists (essentially a tag). Then I can create a smart playlist that is for example all songs that are exclusively in both Happy and Upbeat.

I'm using AppleScripts to keep everything organized and ability to create new artist folders with the click of a button. It's just a huge task and really messy, so I wanted to keep a database setup like this...

ArtistFolderPID
ArtistName
ArtistCleanName
DiscographyPID
EssentialsPID
EPPID
UnreleasedPID
RemixesPID
ftPID\

Logic behind this is each playlist and playlist folder is assigned a unique ID which is what I want to store for each artist. I haven't gotten this far, but I also want to do something similar with the other tags...

I tried working with SQLite in terminal to achieve this, I have the database setup, but passing strings to the terminal requires SO MUCH EFFORT (like the string has to start with sqlite3, spaces are counted as new lines, getting the output...)

Am I going about this like an idiot?? Does anyone have any thoughts?? I've spent the whole weekend trying to figure this out. I stumbled upon some AppleScripts online (like Dougs) which have been SO USEFUL, but I need to be able to customize and streamline it.

Thanks!!

timbot
  • 5
  • 5
  • Okay I think I’m an idiot. You can use Numbers… BUT if anyone knows of a better way, lmk! – timbot Nov 01 '21 at 02:20
  • See **Database Events**. Exists working examples for it on the net. Database Events is designed for just such simple tasks as the one you want to accomplish. Using means of plain AppleScript, without unnecessary complications. – Robert Kniazidis Nov 01 '21 at 06:54
  • Database Events is an object database, not a relational one, so may or may not be appropriate to OP’s needs: it is unstructured and its query support is limited to AppleScript’s `where` clause. (i.e. If OP wish to run full SQL queries then DE is no use.) There are [AppleScript wrappers](https://www.google.com/search?q=applescript+sqlite+wrapper) for SQLite, though this being AppleScript they are probably not great; OP can try them if they wish. (Numbers/Excel isn’t a database either, but is a pretty common choice for this sort of task, so if that’s sufficient it’s probably best.) – foo Nov 01 '21 at 08:27
  • @foo. You finished me off now. If I'm not mistaken, you are the author of the Database Events application. Yes, this is not SQlite3, but as I see it exactly corresponds to what the OP asked for. Plus, it will store the database in smaller files than Numbers or Excel. And there should be a significant gain in speed too. As they say, all ingenious is simple. – Robert Kniazidis Nov 01 '21 at 18:49
  • thanks guys! sorry, yeah I was able to implement it perfectly with numbers, I feel like such a dumbie... – timbot Nov 02 '21 at 03:17
  • @Robert: No, I’m not author of DE. As I say, it may or may not be appropriate to OP’s needs. The same is true of Numbers, which is a spreadsheet app, though a lot of people use spreadsheets as simple single-table databases. Numbers does have the advantage that it has a nice GUI for reading and editing the data, which DE/SQLite don’t. Best choice really depends on whether or not OP wants a GUI, and what if any queries they want to perform on the data. – foo Nov 02 '21 at 07:56
  • @timbot: Nah, there are that many possible ways of doing things, it’s normal to feel totally swamped. Good that you’ve found a solution that works for you. p.s. Are you familiar with [Doug’s Scripts](https://dougscripts.com/itunes/index.php)? Very useful if you do a lot of iTunes/Music scripting. – foo Nov 02 '21 at 07:58
  • Thanks everyone! I have checked out Dougs Scripts. Im a little confused though- is there a way to use it as a library? I've downloaded "manage playlists" and "move playlists to folder", but I would love to access some of the functionality of the scripts. I'm super overwhelmed rn lol, I was thinking about creating an object for artists, but looking into it more I don't think that's the right approach... Im looking into Database Events more and now that I'm getting a better understanding of AppleScripts, I think this would work much better than Numbers! Thank you! Still open to words of wisdom! – timbot Nov 02 '21 at 22:37

1 Answers1

0

Build example (Database Events):

set presidential_data to {¬
    {"Adams", "John", "", 2, 1797, 1801, "Federalist"}, ¬
    {"Adams", "John", "Quincy", 6, 1825, 1829, "Democratic-Republican"}, ¬
    {"Arthur", "Chester", "A.", 21, 1881, 1885, "Republican"}, ¬
    {"Buchanan", "James", "", 15, 1857, 1861, "Democrat"}, ¬
    {"Bush", "George", "H. W.", 41, 1989, 1993, "Republican"}, ¬
    {"Carter", "Jimmy", "", 39, 1977, 1981, "Democrat"}, ¬
    {"Cleveland", "Grover", "", 22, 1885, 1889, "Democratic"}, ¬
    {"Cleveland", "Grover", "", 24, 1893, 1897, "Democratic"}, ¬
    {"Clinton", "William", "J.", 42, 1993, 2001, "Democrat"}, ¬
    {"Coolidge", "Calvin", "", 30, 1923, 1929, "Republican"}, ¬
    {"Eisenhower", "Dwight", "D.", 34, 1953, 1961, "Republican"}, ¬
    {"Fillmore", "Millard", "", 13, 1850, 1853, "Whig"}, ¬
    {"Ford", "Gerald", "R.", 38, 1974, 1977, "Republican"}, ¬
    {"Garfield", "James", "A.", 20, 1881, 1881, "Republican"}, ¬
    {"Grant", "Ulysses", "S.", 18, 1869, 1877, "Republican"}, ¬
    {"Harding", "Warren", "G.", 29, 1921, 1923, "Republican"}, ¬
    {"Harrison", "Benjamin", "", 23, 1889, 1893, "Republican"}, ¬
    {"Harrison", "William", "Henry", 9, 1841, 1841, "Whig"}, ¬
    {"Hayes", "Rutherford", "B.", 19, 1877, 1881, "Republican"}, ¬
    {"Hoover", "Herbert", "C.", 31, 1929, 1933, "Republican"}, ¬
    {"Jackson", "Andrew", "", 7, 1829, 1837, "Democrat"}, ¬
    {"Jefferson", "Thomas", "", 3, 1801, 1809, "Democratic-Republican"}, ¬
    {"Johnson", "Andrew", "", 17, 1865, 1869, "Democrat"}, ¬
    {"Johnson", "Lyndon", "B.", 36, 1963, 1969, "Democrat"}, ¬
    {"Kennedy", "John", "F.", 35, 1961, 1963, "Democrat"}, ¬
    {"Lincoln", "Abraham", "", 16, 1861, 1863, "Republican"}, ¬
    {"Madison", "James", "", 4, 1809, 1817, "Democratic-Republican"}, ¬
    {"McKinley", "William", "", 25, 1897, 1901, "Republican"}, ¬
    {"Monroe", "James", "", 5, 1817, 1825, "Democratic-Republican"}, ¬
    {"Nixon", "Richard", "M.", 37, 1969, 1974, "Republican"}, ¬
    {"Pierce", "Frankin", "", 14, 1853, 1857, "Democrat"}, ¬
    {"Polk", "James", "K.", 11, 1845, 1849, "Democrat"}, ¬
    {"Reagan", "Ronald", "", 40, 1981, 1989, "Republican"}, ¬
    {"Roosevelt", "Frankin", "D.", 32, 1933, 1945, "Democrat"}, ¬
    {"Roosevelt", "Theodore", "", 26, 1901, 1909, "Republican"}, ¬
    {"Taft", "William", "H.", 27, 1909, 1913, "Republican"}, ¬
    {"Taylor", "Zachary", "", 12, 1849, 1860, "Whig"}, ¬
    {"Truman", "Harry", "S.", 33, 1945, 1953, "Democrat"}, ¬
    {"Tyler", "john", "", 10, 1841, 1845, "Whig"}, ¬
    {"Van Buren", "Martin", "", 8, 1837, 1841, "Democrat"}, ¬
    {"Washington", "George", "", 1, 1789, 1797, "None"}, ¬
    {"Wilson", "Woodrow", "", 28, 1913, 1921, "Democrat"}}

-- field names must match the order of the data
set the field_names to {"Last Name", "First Name", "Middle Initials", "Index", "Start Year", "End Year", "Party"}

-- database file names have a name extension of "dbev"
set database_name to "Presidents"

tell application "Database Events"
    if (count of databases) is greater than 0 then
        quit
    end if
end tell

-- build the database
-- NOTE: due to a bug in Database Events, all database files are created with a name extension of "dbev" in a directory named Databases in the Documents folder
tell application "Database Events"
    set this_database to make new database with properties {name:database_name}
    tell this_database
        repeat with record_data in the presidential_data
            set this_record to make new record with properties {name:""}
            tell this_record
                repeat with i from 1 to the count of field_names
                    set the field_name to item i of field_names
                    set the field_data to item i of record_data
                    make new field with properties {name:field_name, value:field_data}
                end repeat
            end tell
        end repeat
        save
    end tell
    quit
end tell

Query Examples (reading the created database):

set database_file to (POSIX path of (path to documents folder)) & "Databases/presidents.dbev"

tell application "Database Events"
    tell database database_file
        get value of field "Last Name" of (every record whose value of field "Start Year" is greater than or equal to 1800 and value of field "Start Year" is less than or equal to 1900)
        -- {"Adams", "Arthur", "Buchanan", "Cleveland", "Cleveland", "Fillmore", "Garfield", "Grant", "Harrison", "Harrison", "Hayes", "Jackson", "Jefferson", "Johnson", "Lincoln", "Madison", "McKinley", "Monroe", "Pierce", "Polk", "Taylor", "Tyler", "Van Buren"}
        get value of field "Last Name" of (every record whose value of field "Party" is "Republican")
        get value of field "Last Name" of (every record whose value of field "Start Year" is greater than 1899)
        get the first record whose value of field "Last Name" contains "Adams"
        get the value of field "Last Name" of every record
    end tell
end tell
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8
  • Note: The first script in this answer fails if `~/Documents/Databases/` does not already exist, which it does not by default. – user3439894 Nov 01 '21 at 14:56