AppleScript to download file












-1















I need to write an AppleScript to open Safari and go to https://www.stats.govt.nz/large-datasets/csv-files-for-download/ to download the first csv file










share|improve this question


















  • 1





    This is not a question, it is outsourcing a task or homework.

    – RalfFriedl
    Nov 21 '18 at 6:47






  • 1





    How To Ask

    – Halil İbrahim
    Nov 21 '18 at 6:48
















-1















I need to write an AppleScript to open Safari and go to https://www.stats.govt.nz/large-datasets/csv-files-for-download/ to download the first csv file










share|improve this question


















  • 1





    This is not a question, it is outsourcing a task or homework.

    – RalfFriedl
    Nov 21 '18 at 6:47






  • 1





    How To Ask

    – Halil İbrahim
    Nov 21 '18 at 6:48














-1












-1








-1








I need to write an AppleScript to open Safari and go to https://www.stats.govt.nz/large-datasets/csv-files-for-download/ to download the first csv file










share|improve this question














I need to write an AppleScript to open Safari and go to https://www.stats.govt.nz/large-datasets/csv-files-for-download/ to download the first csv file







applescript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 6:45









SpringSpring

1




1








  • 1





    This is not a question, it is outsourcing a task or homework.

    – RalfFriedl
    Nov 21 '18 at 6:47






  • 1





    How To Ask

    – Halil İbrahim
    Nov 21 '18 at 6:48














  • 1





    This is not a question, it is outsourcing a task or homework.

    – RalfFriedl
    Nov 21 '18 at 6:47






  • 1





    How To Ask

    – Halil İbrahim
    Nov 21 '18 at 6:48








1




1





This is not a question, it is outsourcing a task or homework.

– RalfFriedl
Nov 21 '18 at 6:47





This is not a question, it is outsourcing a task or homework.

– RalfFriedl
Nov 21 '18 at 6:47




1




1





How To Ask

– Halil İbrahim
Nov 21 '18 at 6:48





How To Ask

– Halil İbrahim
Nov 21 '18 at 6:48












2 Answers
2






active

oldest

votes


















0














Rather than doing your work for you by providing you with a code snippet to accomplish this task, I will instead provide you with a great resource. Cube MG has a bunch of tutorials that are really helpful for interacting with web page elements via Safari. Check it out here: http://www.cubemg.com/how-to-click-a-button-on-a-web-page-with-applescript/



There are other ways that you could approach something like this such as using the command line but that is a bit more advanced. Check out Cube MG and see what you can learn!






share|improve this answer































    0














    tell application "Safari"
    make new document with properties ¬
    {URL:"https://www.stats.govt.nz/large-datasets/csv-files-for-download/"}

    repeat
    if the front document's source contains "What are CSV files?" then ¬
    exit repeat

    delay 0.5
    end repeat

    tell the front document to set CSVfiles to ¬
    do JavaScript "Array.from(document.links,
    x=>x.href)
    .filter(x=>x.indexOf('.csv')!=-1);"
    end tell


    This gets you the links to all the CSV files. To extract the first one, use item 1 of CSVfiles.





    NB. Don't forget to enable Allow JavaScript from Apple Events in Developer menu (a hidden menu, made visible through Preferences > Advanced > Show Develop menu in menu bar)






    share|improve this answer


























    • A few things about your answer: 1. Copy and pasting the code as is into Script Editor errors on compile, Expected expression but found “tell”.. Had to change set CSVfiles to tell the front document to ¬ to tell the front document to set CSVfiles to ¬ for it to work. 2. repeat while the name of front document = "Untitled" is unreliable as the name of document 1 can change before the page is completely loaded and {} can/will be returned for the JavaScript command. 3. Allow JavaScript from Apple Events on the hidden Developer menu in Safari needs to be checked, which isn't by default.

      – user3439894
      Nov 24 '18 at 7:09











    • Many thanks, @user3439894

      – CJK
      Nov 24 '18 at 7:15











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406580%2fapplescript-to-download-file%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Rather than doing your work for you by providing you with a code snippet to accomplish this task, I will instead provide you with a great resource. Cube MG has a bunch of tutorials that are really helpful for interacting with web page elements via Safari. Check it out here: http://www.cubemg.com/how-to-click-a-button-on-a-web-page-with-applescript/



    There are other ways that you could approach something like this such as using the command line but that is a bit more advanced. Check out Cube MG and see what you can learn!






    share|improve this answer




























      0














      Rather than doing your work for you by providing you with a code snippet to accomplish this task, I will instead provide you with a great resource. Cube MG has a bunch of tutorials that are really helpful for interacting with web page elements via Safari. Check it out here: http://www.cubemg.com/how-to-click-a-button-on-a-web-page-with-applescript/



      There are other ways that you could approach something like this such as using the command line but that is a bit more advanced. Check out Cube MG and see what you can learn!






      share|improve this answer


























        0












        0








        0







        Rather than doing your work for you by providing you with a code snippet to accomplish this task, I will instead provide you with a great resource. Cube MG has a bunch of tutorials that are really helpful for interacting with web page elements via Safari. Check it out here: http://www.cubemg.com/how-to-click-a-button-on-a-web-page-with-applescript/



        There are other ways that you could approach something like this such as using the command line but that is a bit more advanced. Check out Cube MG and see what you can learn!






        share|improve this answer













        Rather than doing your work for you by providing you with a code snippet to accomplish this task, I will instead provide you with a great resource. Cube MG has a bunch of tutorials that are really helpful for interacting with web page elements via Safari. Check it out here: http://www.cubemg.com/how-to-click-a-button-on-a-web-page-with-applescript/



        There are other ways that you could approach something like this such as using the command line but that is a bit more advanced. Check out Cube MG and see what you can learn!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 12:56









        Howard DesignsHoward Designs

        1




        1

























            0














            tell application "Safari"
            make new document with properties ¬
            {URL:"https://www.stats.govt.nz/large-datasets/csv-files-for-download/"}

            repeat
            if the front document's source contains "What are CSV files?" then ¬
            exit repeat

            delay 0.5
            end repeat

            tell the front document to set CSVfiles to ¬
            do JavaScript "Array.from(document.links,
            x=>x.href)
            .filter(x=>x.indexOf('.csv')!=-1);"
            end tell


            This gets you the links to all the CSV files. To extract the first one, use item 1 of CSVfiles.





            NB. Don't forget to enable Allow JavaScript from Apple Events in Developer menu (a hidden menu, made visible through Preferences > Advanced > Show Develop menu in menu bar)






            share|improve this answer


























            • A few things about your answer: 1. Copy and pasting the code as is into Script Editor errors on compile, Expected expression but found “tell”.. Had to change set CSVfiles to tell the front document to ¬ to tell the front document to set CSVfiles to ¬ for it to work. 2. repeat while the name of front document = "Untitled" is unreliable as the name of document 1 can change before the page is completely loaded and {} can/will be returned for the JavaScript command. 3. Allow JavaScript from Apple Events on the hidden Developer menu in Safari needs to be checked, which isn't by default.

              – user3439894
              Nov 24 '18 at 7:09











            • Many thanks, @user3439894

              – CJK
              Nov 24 '18 at 7:15
















            0














            tell application "Safari"
            make new document with properties ¬
            {URL:"https://www.stats.govt.nz/large-datasets/csv-files-for-download/"}

            repeat
            if the front document's source contains "What are CSV files?" then ¬
            exit repeat

            delay 0.5
            end repeat

            tell the front document to set CSVfiles to ¬
            do JavaScript "Array.from(document.links,
            x=>x.href)
            .filter(x=>x.indexOf('.csv')!=-1);"
            end tell


            This gets you the links to all the CSV files. To extract the first one, use item 1 of CSVfiles.





            NB. Don't forget to enable Allow JavaScript from Apple Events in Developer menu (a hidden menu, made visible through Preferences > Advanced > Show Develop menu in menu bar)






            share|improve this answer


























            • A few things about your answer: 1. Copy and pasting the code as is into Script Editor errors on compile, Expected expression but found “tell”.. Had to change set CSVfiles to tell the front document to ¬ to tell the front document to set CSVfiles to ¬ for it to work. 2. repeat while the name of front document = "Untitled" is unreliable as the name of document 1 can change before the page is completely loaded and {} can/will be returned for the JavaScript command. 3. Allow JavaScript from Apple Events on the hidden Developer menu in Safari needs to be checked, which isn't by default.

              – user3439894
              Nov 24 '18 at 7:09











            • Many thanks, @user3439894

              – CJK
              Nov 24 '18 at 7:15














            0












            0








            0







            tell application "Safari"
            make new document with properties ¬
            {URL:"https://www.stats.govt.nz/large-datasets/csv-files-for-download/"}

            repeat
            if the front document's source contains "What are CSV files?" then ¬
            exit repeat

            delay 0.5
            end repeat

            tell the front document to set CSVfiles to ¬
            do JavaScript "Array.from(document.links,
            x=>x.href)
            .filter(x=>x.indexOf('.csv')!=-1);"
            end tell


            This gets you the links to all the CSV files. To extract the first one, use item 1 of CSVfiles.





            NB. Don't forget to enable Allow JavaScript from Apple Events in Developer menu (a hidden menu, made visible through Preferences > Advanced > Show Develop menu in menu bar)






            share|improve this answer















            tell application "Safari"
            make new document with properties ¬
            {URL:"https://www.stats.govt.nz/large-datasets/csv-files-for-download/"}

            repeat
            if the front document's source contains "What are CSV files?" then ¬
            exit repeat

            delay 0.5
            end repeat

            tell the front document to set CSVfiles to ¬
            do JavaScript "Array.from(document.links,
            x=>x.href)
            .filter(x=>x.indexOf('.csv')!=-1);"
            end tell


            This gets you the links to all the CSV files. To extract the first one, use item 1 of CSVfiles.





            NB. Don't forget to enable Allow JavaScript from Apple Events in Developer menu (a hidden menu, made visible through Preferences > Advanced > Show Develop menu in menu bar)







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 24 '18 at 10:36

























            answered Nov 24 '18 at 2:57









            CJKCJK

            2,5981216




            2,5981216













            • A few things about your answer: 1. Copy and pasting the code as is into Script Editor errors on compile, Expected expression but found “tell”.. Had to change set CSVfiles to tell the front document to ¬ to tell the front document to set CSVfiles to ¬ for it to work. 2. repeat while the name of front document = "Untitled" is unreliable as the name of document 1 can change before the page is completely loaded and {} can/will be returned for the JavaScript command. 3. Allow JavaScript from Apple Events on the hidden Developer menu in Safari needs to be checked, which isn't by default.

              – user3439894
              Nov 24 '18 at 7:09











            • Many thanks, @user3439894

              – CJK
              Nov 24 '18 at 7:15



















            • A few things about your answer: 1. Copy and pasting the code as is into Script Editor errors on compile, Expected expression but found “tell”.. Had to change set CSVfiles to tell the front document to ¬ to tell the front document to set CSVfiles to ¬ for it to work. 2. repeat while the name of front document = "Untitled" is unreliable as the name of document 1 can change before the page is completely loaded and {} can/will be returned for the JavaScript command. 3. Allow JavaScript from Apple Events on the hidden Developer menu in Safari needs to be checked, which isn't by default.

              – user3439894
              Nov 24 '18 at 7:09











            • Many thanks, @user3439894

              – CJK
              Nov 24 '18 at 7:15

















            A few things about your answer: 1. Copy and pasting the code as is into Script Editor errors on compile, Expected expression but found “tell”.. Had to change set CSVfiles to tell the front document to ¬ to tell the front document to set CSVfiles to ¬ for it to work. 2. repeat while the name of front document = "Untitled" is unreliable as the name of document 1 can change before the page is completely loaded and {} can/will be returned for the JavaScript command. 3. Allow JavaScript from Apple Events on the hidden Developer menu in Safari needs to be checked, which isn't by default.

            – user3439894
            Nov 24 '18 at 7:09





            A few things about your answer: 1. Copy and pasting the code as is into Script Editor errors on compile, Expected expression but found “tell”.. Had to change set CSVfiles to tell the front document to ¬ to tell the front document to set CSVfiles to ¬ for it to work. 2. repeat while the name of front document = "Untitled" is unreliable as the name of document 1 can change before the page is completely loaded and {} can/will be returned for the JavaScript command. 3. Allow JavaScript from Apple Events on the hidden Developer menu in Safari needs to be checked, which isn't by default.

            – user3439894
            Nov 24 '18 at 7:09













            Many thanks, @user3439894

            – CJK
            Nov 24 '18 at 7:15





            Many thanks, @user3439894

            – CJK
            Nov 24 '18 at 7:15


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406580%2fapplescript-to-download-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud