Help me write an AppleScript?

Discussion in 'Mac Programming' started by lava_man, Nov 14, 2016.

  1. lava_man macrumors newbie

    lava_man

    Joined:
    Nov 14, 2016
    #1
    I've got a folder full of .CSV files containing my playlist information from Spotify premium that I need to edit before I can upload said playlists into my Google Play Music.

    Basically in every file, column "A" needs to be deleted, then the resulting field 1 in column(s) "A" through "D" need to be renamed track, artist, album, and playlist, in that order.

    In column "D," all fields under playlist (D2 through however many rows are in the file) I would like the filename to be entered.

    Lastly, columns E thru I are unnecessary and therefore should be deleted as well.

    I tried "watch me do" in automator, to no avail. So then I began to search through the functions in automator, and couldn't find any sort of actions that would allow me to make such changes.
     
  2. mijail macrumors 6502

    Joined:
    Oct 31, 2010
    #2
    My advice: don't use AppleScript, unless your goal is to directly interface with applications which do publish an AppleScript dictionary - and even then, I would try using the Javascript variant instead of Applescript proper. There seems to be less information online about how to do that than for AppleScript, but still I bet it will be less frustrating/more productive in the mid and long term.

    Anyway, what you want to do sounds like could be done manually in any spreadsheet program in 3 minutes. If you really want to do it with a script, I'd try Python - or Ruby, or Javascript.
     
  3. jaduff46 macrumors regular

    jaduff46

    Joined:
    Mar 3, 2010
    Location:
    Northern NJ
    #3
    awk would also probably work for this, especially if you're processing a large number of files with the same format.

    i am, however, not familiar with the file format from Spotify
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Post a sample of the actual data, say at least 25 lines.

    Please use CODE tags when posting the data,
    OR
    Add a text file as an attachment to your post.


    Given just your description, it sounds eminently doable in 'awk'. But to create an awk program, one needs data to be processed. While one could create test data, it's best to use the actual data in question.


    Also, since you say the data is in multiple CSV files, post the list of files to be processed. We need to see if they follow a naming convention, such as all having a ".csv" extension, or whether something else will be needed. To list all the files in a folder, type this into a Terminal window, with a single space at the end, and no RETURN key:
    Code:
    ls -l 
    Then drag the folder containing the CSV files over the Terminal window and drop it. Terminal should expand the dropped folder to the pathname of that folder.

    Then activate the Terminal window and press the RETURN key.

    Copy and paste the output, with CODE tags around it.
     
  5. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #5
    Sounds like chown33 has the most elegant approach. I agree that AppleScript is probably not the way to go.

    You can probably use Automator to add a slightly more user friendly front end onto the awk command if this is something you're likely to do several times again.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Yup, it's in my plan.

    Automator is a good way of turning a command-line into a drag-n-drop app. It's also a good way to make Folder Actions or just a double-clickable app from a command-line.
     
  7. mijail macrumors 6502

    Joined:
    Oct 31, 2010
    #7
    Oh God. I recommended not to use AppleScript because it's a pain - and people are recommending AWK??

    There are tons of modern, in use, non-obscure, non-broken scripting languages. I already mentioned Python, Ruby, Javascript. There's still Lua, TCL - most of them famously easy to get into, and even with options for GUIs and full programming facilities (will you want to automatize the uploads after you finish processing those lists?).

    But still people go for the 40-year old beasts with multiple, partially incompatible versions, which start their manuals with a "Dark Corners" section?

    Well, at least no-one recommended Bash - yet! :D
     
  8. MacUser2525 macrumors 68000

    MacUser2525

    Joined:
    Mar 17, 2007
    Location:
    Canada
    #8
    Well I would have started this idea for myself with a bash script that processed all the files with one of them stodgy old time tested and proven commands you denigrate in your post.
     
  9. mijail macrumors 6502

    Joined:
    Oct 31, 2010
    #9
    Yeah, exactly as I have done too many times - and the result is always the same: too much time and hair wasted for the functionality I got.

    If the OP is going to *learn* a scripting language, going to AWK is idiotic. It's in itself less idiotic than learning Bash, but since he will *also* need Bash really soon to do anything non-trivial with AWK, the hole of idiocy starts to grow.

    Meanwhile, someone who started in Python, or JavaScript, or lots of others, could do things in a sane environment and grow the program without any limit. And even if he only wants limited functionality I bet he would end faster.

    Now, if the OP already knew AWK and/or Bash *and* can do his thing in less than a handful of lines as a one-shot thing, then *maybe* it'll be OK. But then he wouldn't be asking. And if the task was so easy anyway, he could have done it in 3 minutes manually in any spreadsheet program - as I already said earlier.

    For the record: I *wish* that whenever I was about to start any of my AppleScript of Bash scripts someone had whacked me over the head until I used *anything* saner. Which is why I am doing it now.
     
  10. MacUser2525, Dec 1, 2016
    Last edited: Dec 1, 2016

    MacUser2525 macrumors 68000

    MacUser2525

    Joined:
    Mar 17, 2007
    Location:
    Canada
    #10
    The same can be said of any of those other languages you refer to as well, best he just sit back and be spoon fed a GUI by someone and never learn how his computer works.

    Edit: I would add this is what awk was designed for cutting out fields/columns in a text file on a command line or in script. The old *nix one tool for the job that does it well theory that is the basis of its design philosophy originally..
     
  11. mijail macrumors 6502

    Joined:
    Oct 31, 2010
    #11
    Do any of those languages *start* their manual with a "Dark corners" warning and quote from the language creator, like AWK does?

    Bash is even worse! :D

    I'm talking here about using languages that can *create* GUIs (unlike AWK or Bash) - and you talk about being spoon-fed GUIs? ... what??

    And, pray tell, how will AWK or Bash help anyone learn "how the computer works"? :D
     
  12. chown33, Dec 1, 2016
    Last edited: Dec 1, 2016

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #12
    I agree with you. I'd qualify it slightly, however, as "going to AWK first is idiotic".

    Good thing I didn't actually suggest he learn awk. I suggested that awk was capable of doing this. And since I know awk, I was willing to contribute code to solving this particular problem. The OP didn't need to learn awk, and any awk I posted I was completely willing to explain.

    Automator was the second prong in my plan. See my above post.

    However, since the OP hasn't posted any data, and the description is a bit vague for me to start thrashing at it or fabricating sample data, I don't see any point to dragging this out further. I have no interest in arguing about the most suitable language to learn first, or to solve this particular problem. Until some actionable data is posted, I'm out.
     
  13. jaduff46 macrumors regular

    jaduff46

    Joined:
    Mar 3, 2010
    Location:
    Northern NJ
    #13
    This is why no further comment from me after chown33's original suggestion.

    Right tools, don't need Python or another language, easy to test (dump into Excel and see if the formatted output is correct).

    JD
     
  14. Zazoh macrumors 6502a

    Zazoh

    Joined:
    Jan 4, 2009
    Location:
    Mico, Texas
    #14
    I've been programming for 20 years -- One reason I love it -- if the solution is agnostic to the problem, there is more than one way to skin a cat. Almost NOTHING I do anymore, is from scratch, so its whatever tools I have and how close it is to another solution == productivity.
     
  15. mijail macrumors 6502

    Joined:
    Oct 31, 2010
    #15
  16. jaduff46 macrumors regular

    jaduff46

    Joined:
    Mar 3, 2010
    Location:
    Northern NJ
    #16
    Over 43 myself before retiring last year. BAL and PL/1 were the first. Still do some occasional Excel/VBA to help people.

    What was the question again?
     
  17. mijail macrumors 6502

    Joined:
    Oct 31, 2010
    #17
    Mine is, how do pissing contests help decide the merit of tools?
     

Share This Page