When writing to an SQLite file in AppleScript 2.2 with application Database Events

565 Views Asked by At

I am making an AppleScript application with AppleScript 2.2 on Mac OS X 10.7 (Build: 11A511) Lion. What my application is doing is capturing the current iTunes song and storing it if the song is different from the last one. Then I told it to take the the current song and place it into an SQLite file using Database Events. Basically it is adding the current song to a new field, but when the song changes it says that there are no old fields and then writes the song (the process repeats...); I do have a save after it makes the field.

My code is as follows:

        if currentsong is not equal to previous_song then

    tell application "Database Events"
        tell database "songlist"
            set song to make new record with properties {name:"songs"}
            set song_count to count fields
            tell song
                make new field with properties {name:currentsong, value:song_count + 1}
            end tell
        end tell
        save database "songlist"
    end tell

end if
1

There are 1 best solutions below

2
On BEST ANSWER

It seems you're a little confused by records and fields. A database has records and a record has fields. In your code you are telling the database to count the fields... which is a problem because a database has records, not fields.

So change "fields" to "records" where you set the song_count. Also, if you want that count to be correct then put that line ahead of where you create the new record because you're trying to get the count before you add a new record.