Saving a page and clicking on the save popup in chrome using AppleScript












0















I want to download a txt file, opened in a tab in chrome using AppleScript. I want the save as dialog of Mac to provide the default extension and the name of the file.



tell application "Google Chrome" to tell active tab of window 1 to save as "r1.txt"


I tried this approach, and some other approaches like



activate application "Google Chrome"
tell application "System Events"
tell process "chrome"
keystroke "s" using {command down}
delay 1
click button "Save" of sheet 1 of window 1
end tell
end tell


still am not able to click the save button in the modal.










share|improve this question



























    0















    I want to download a txt file, opened in a tab in chrome using AppleScript. I want the save as dialog of Mac to provide the default extension and the name of the file.



    tell application "Google Chrome" to tell active tab of window 1 to save as "r1.txt"


    I tried this approach, and some other approaches like



    activate application "Google Chrome"
    tell application "System Events"
    tell process "chrome"
    keystroke "s" using {command down}
    delay 1
    click button "Save" of sheet 1 of window 1
    end tell
    end tell


    still am not able to click the save button in the modal.










    share|improve this question

























      0












      0








      0


      1






      I want to download a txt file, opened in a tab in chrome using AppleScript. I want the save as dialog of Mac to provide the default extension and the name of the file.



      tell application "Google Chrome" to tell active tab of window 1 to save as "r1.txt"


      I tried this approach, and some other approaches like



      activate application "Google Chrome"
      tell application "System Events"
      tell process "chrome"
      keystroke "s" using {command down}
      delay 1
      click button "Save" of sheet 1 of window 1
      end tell
      end tell


      still am not able to click the save button in the modal.










      share|improve this question














      I want to download a txt file, opened in a tab in chrome using AppleScript. I want the save as dialog of Mac to provide the default extension and the name of the file.



      tell application "Google Chrome" to tell active tab of window 1 to save as "r1.txt"


      I tried this approach, and some other approaches like



      activate application "Google Chrome"
      tell application "System Events"
      tell process "chrome"
      keystroke "s" using {command down}
      delay 1
      click button "Save" of sheet 1 of window 1
      end tell
      end tell


      still am not able to click the save button in the modal.







      google-chrome applescript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 15:22









      mmrmmr

      1




      1
























          2 Answers
          2






          active

          oldest

          votes


















          0














          This works for me using the latest version of Google Chrome and the latest version of MacOS Mojave



          activate application "Google Chrome"
          tell application "System Events"
          repeat while not (exists of menu bar item "File" of menu bar 1 of application process "Chrome")
          delay 0.1
          end repeat
          click menu bar item "File" of menu bar 1 of application process "Chrome"
          repeat while not (exists of menu item 11 of menu 1 of menu bar item "File" of menu bar 1 of application process "Chrome")
          delay 0.1
          end repeat
          click menu item 11 of menu 1 of menu bar item "File" of menu bar 1 of application process "Chrome"
          repeat while not (exists of UI element "Save" of sheet 1 of window 1 of application process "Chrome")
          delay 0.1
          end repeat
          click UI element "Save" of sheet 1 of window 1 of application process "Chrome"
          end tell





          share|improve this answer





















          • 1





            "You may need to adjust the delay values" Rather, you could improve the robustness of this script by testing for object existence instead of guessing how long it might take an object to be created. It'll be slightly bulkier code but it would help promote good coding practice to newcomers learning from your example.

            – CJK
            Nov 13 '18 at 16:58











          • Point taken and agreed

            – wch1zpink
            Nov 13 '18 at 17:08



















          0














          My approach to this problem was to try and avoid scripting the UI if possible, which can be problematic and unreliable. Instead, I decided to use the shell command curl to do the downloading job for us instead of trying to manipulate Chrome into doing it.



          All we need is the location of where to save the file to, which I've set as the location to which Google Chrome defaults to, namely ~/Downloads.



          property path : "~/Downloads" -- Where to download the file to

          use Chrome : application "Google Chrome"
          property sys : application "System Events"

          property window : a reference to window 1 of Chrome
          property tab : a reference to active tab of my window
          property URL : a reference to URL of my tab

          property text item delimiters : {space, "/"}

          on run
          -- Stop the script if there's no URL to grab
          if not (my URL exists) then return false

          -- Path to where the file will be saved
          set HFSPath to the path of sys's item (my path)
          -- Dereferencing the URL
          set www to my URL as text
          -- Extract the filename portion of the URL
          set filename to the last text item of www

          -- The shell script to grab the contents of a URL
          set sh to the contents of {¬
          "cd", quoted form of the POSIX path of HFSPath, ";", ¬
          "curl --remote-name", ¬
          "--url", quoted form of www} as text


          ## 1. Download the file
          try
          using terms from scripting additions
          do shell script sh
          end using terms from
          on error E
          return E
          end try


          ## 2. Reveal the downloaded file in Finder
          tell application "Finder"
          tell the file named filename in the ¬
          folder named HFSPath to if ¬
          it exists then reveal it
          activate
          end tell
          end run


          It's a longer script than your present one, but most of it is declarations of variables (and properties), after which the script does two simple things:




          1. Grabs the URL of the active tab in Chrome, and downloads the contents of that URL into the specified folder, retaining the same filename and extension as the remote file;


          2. Once the download is complete, it reveals the file in Finder.







          share|improve this answer























            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%2f53284192%2fsaving-a-page-and-clicking-on-the-save-popup-in-chrome-using-applescript%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














            This works for me using the latest version of Google Chrome and the latest version of MacOS Mojave



            activate application "Google Chrome"
            tell application "System Events"
            repeat while not (exists of menu bar item "File" of menu bar 1 of application process "Chrome")
            delay 0.1
            end repeat
            click menu bar item "File" of menu bar 1 of application process "Chrome"
            repeat while not (exists of menu item 11 of menu 1 of menu bar item "File" of menu bar 1 of application process "Chrome")
            delay 0.1
            end repeat
            click menu item 11 of menu 1 of menu bar item "File" of menu bar 1 of application process "Chrome"
            repeat while not (exists of UI element "Save" of sheet 1 of window 1 of application process "Chrome")
            delay 0.1
            end repeat
            click UI element "Save" of sheet 1 of window 1 of application process "Chrome"
            end tell





            share|improve this answer





















            • 1





              "You may need to adjust the delay values" Rather, you could improve the robustness of this script by testing for object existence instead of guessing how long it might take an object to be created. It'll be slightly bulkier code but it would help promote good coding practice to newcomers learning from your example.

              – CJK
              Nov 13 '18 at 16:58











            • Point taken and agreed

              – wch1zpink
              Nov 13 '18 at 17:08
















            0














            This works for me using the latest version of Google Chrome and the latest version of MacOS Mojave



            activate application "Google Chrome"
            tell application "System Events"
            repeat while not (exists of menu bar item "File" of menu bar 1 of application process "Chrome")
            delay 0.1
            end repeat
            click menu bar item "File" of menu bar 1 of application process "Chrome"
            repeat while not (exists of menu item 11 of menu 1 of menu bar item "File" of menu bar 1 of application process "Chrome")
            delay 0.1
            end repeat
            click menu item 11 of menu 1 of menu bar item "File" of menu bar 1 of application process "Chrome"
            repeat while not (exists of UI element "Save" of sheet 1 of window 1 of application process "Chrome")
            delay 0.1
            end repeat
            click UI element "Save" of sheet 1 of window 1 of application process "Chrome"
            end tell





            share|improve this answer





















            • 1





              "You may need to adjust the delay values" Rather, you could improve the robustness of this script by testing for object existence instead of guessing how long it might take an object to be created. It'll be slightly bulkier code but it would help promote good coding practice to newcomers learning from your example.

              – CJK
              Nov 13 '18 at 16:58











            • Point taken and agreed

              – wch1zpink
              Nov 13 '18 at 17:08














            0












            0








            0







            This works for me using the latest version of Google Chrome and the latest version of MacOS Mojave



            activate application "Google Chrome"
            tell application "System Events"
            repeat while not (exists of menu bar item "File" of menu bar 1 of application process "Chrome")
            delay 0.1
            end repeat
            click menu bar item "File" of menu bar 1 of application process "Chrome"
            repeat while not (exists of menu item 11 of menu 1 of menu bar item "File" of menu bar 1 of application process "Chrome")
            delay 0.1
            end repeat
            click menu item 11 of menu 1 of menu bar item "File" of menu bar 1 of application process "Chrome"
            repeat while not (exists of UI element "Save" of sheet 1 of window 1 of application process "Chrome")
            delay 0.1
            end repeat
            click UI element "Save" of sheet 1 of window 1 of application process "Chrome"
            end tell





            share|improve this answer















            This works for me using the latest version of Google Chrome and the latest version of MacOS Mojave



            activate application "Google Chrome"
            tell application "System Events"
            repeat while not (exists of menu bar item "File" of menu bar 1 of application process "Chrome")
            delay 0.1
            end repeat
            click menu bar item "File" of menu bar 1 of application process "Chrome"
            repeat while not (exists of menu item 11 of menu 1 of menu bar item "File" of menu bar 1 of application process "Chrome")
            delay 0.1
            end repeat
            click menu item 11 of menu 1 of menu bar item "File" of menu bar 1 of application process "Chrome"
            repeat while not (exists of UI element "Save" of sheet 1 of window 1 of application process "Chrome")
            delay 0.1
            end repeat
            click UI element "Save" of sheet 1 of window 1 of application process "Chrome"
            end tell






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 17:06

























            answered Nov 13 '18 at 16:20









            wch1zpinkwch1zpink

            1,1411414




            1,1411414








            • 1





              "You may need to adjust the delay values" Rather, you could improve the robustness of this script by testing for object existence instead of guessing how long it might take an object to be created. It'll be slightly bulkier code but it would help promote good coding practice to newcomers learning from your example.

              – CJK
              Nov 13 '18 at 16:58











            • Point taken and agreed

              – wch1zpink
              Nov 13 '18 at 17:08














            • 1





              "You may need to adjust the delay values" Rather, you could improve the robustness of this script by testing for object existence instead of guessing how long it might take an object to be created. It'll be slightly bulkier code but it would help promote good coding practice to newcomers learning from your example.

              – CJK
              Nov 13 '18 at 16:58











            • Point taken and agreed

              – wch1zpink
              Nov 13 '18 at 17:08








            1




            1





            "You may need to adjust the delay values" Rather, you could improve the robustness of this script by testing for object existence instead of guessing how long it might take an object to be created. It'll be slightly bulkier code but it would help promote good coding practice to newcomers learning from your example.

            – CJK
            Nov 13 '18 at 16:58





            "You may need to adjust the delay values" Rather, you could improve the robustness of this script by testing for object existence instead of guessing how long it might take an object to be created. It'll be slightly bulkier code but it would help promote good coding practice to newcomers learning from your example.

            – CJK
            Nov 13 '18 at 16:58













            Point taken and agreed

            – wch1zpink
            Nov 13 '18 at 17:08





            Point taken and agreed

            – wch1zpink
            Nov 13 '18 at 17:08













            0














            My approach to this problem was to try and avoid scripting the UI if possible, which can be problematic and unreliable. Instead, I decided to use the shell command curl to do the downloading job for us instead of trying to manipulate Chrome into doing it.



            All we need is the location of where to save the file to, which I've set as the location to which Google Chrome defaults to, namely ~/Downloads.



            property path : "~/Downloads" -- Where to download the file to

            use Chrome : application "Google Chrome"
            property sys : application "System Events"

            property window : a reference to window 1 of Chrome
            property tab : a reference to active tab of my window
            property URL : a reference to URL of my tab

            property text item delimiters : {space, "/"}

            on run
            -- Stop the script if there's no URL to grab
            if not (my URL exists) then return false

            -- Path to where the file will be saved
            set HFSPath to the path of sys's item (my path)
            -- Dereferencing the URL
            set www to my URL as text
            -- Extract the filename portion of the URL
            set filename to the last text item of www

            -- The shell script to grab the contents of a URL
            set sh to the contents of {¬
            "cd", quoted form of the POSIX path of HFSPath, ";", ¬
            "curl --remote-name", ¬
            "--url", quoted form of www} as text


            ## 1. Download the file
            try
            using terms from scripting additions
            do shell script sh
            end using terms from
            on error E
            return E
            end try


            ## 2. Reveal the downloaded file in Finder
            tell application "Finder"
            tell the file named filename in the ¬
            folder named HFSPath to if ¬
            it exists then reveal it
            activate
            end tell
            end run


            It's a longer script than your present one, but most of it is declarations of variables (and properties), after which the script does two simple things:




            1. Grabs the URL of the active tab in Chrome, and downloads the contents of that URL into the specified folder, retaining the same filename and extension as the remote file;


            2. Once the download is complete, it reveals the file in Finder.







            share|improve this answer




























              0














              My approach to this problem was to try and avoid scripting the UI if possible, which can be problematic and unreliable. Instead, I decided to use the shell command curl to do the downloading job for us instead of trying to manipulate Chrome into doing it.



              All we need is the location of where to save the file to, which I've set as the location to which Google Chrome defaults to, namely ~/Downloads.



              property path : "~/Downloads" -- Where to download the file to

              use Chrome : application "Google Chrome"
              property sys : application "System Events"

              property window : a reference to window 1 of Chrome
              property tab : a reference to active tab of my window
              property URL : a reference to URL of my tab

              property text item delimiters : {space, "/"}

              on run
              -- Stop the script if there's no URL to grab
              if not (my URL exists) then return false

              -- Path to where the file will be saved
              set HFSPath to the path of sys's item (my path)
              -- Dereferencing the URL
              set www to my URL as text
              -- Extract the filename portion of the URL
              set filename to the last text item of www

              -- The shell script to grab the contents of a URL
              set sh to the contents of {¬
              "cd", quoted form of the POSIX path of HFSPath, ";", ¬
              "curl --remote-name", ¬
              "--url", quoted form of www} as text


              ## 1. Download the file
              try
              using terms from scripting additions
              do shell script sh
              end using terms from
              on error E
              return E
              end try


              ## 2. Reveal the downloaded file in Finder
              tell application "Finder"
              tell the file named filename in the ¬
              folder named HFSPath to if ¬
              it exists then reveal it
              activate
              end tell
              end run


              It's a longer script than your present one, but most of it is declarations of variables (and properties), after which the script does two simple things:




              1. Grabs the URL of the active tab in Chrome, and downloads the contents of that URL into the specified folder, retaining the same filename and extension as the remote file;


              2. Once the download is complete, it reveals the file in Finder.







              share|improve this answer


























                0












                0








                0







                My approach to this problem was to try and avoid scripting the UI if possible, which can be problematic and unreliable. Instead, I decided to use the shell command curl to do the downloading job for us instead of trying to manipulate Chrome into doing it.



                All we need is the location of where to save the file to, which I've set as the location to which Google Chrome defaults to, namely ~/Downloads.



                property path : "~/Downloads" -- Where to download the file to

                use Chrome : application "Google Chrome"
                property sys : application "System Events"

                property window : a reference to window 1 of Chrome
                property tab : a reference to active tab of my window
                property URL : a reference to URL of my tab

                property text item delimiters : {space, "/"}

                on run
                -- Stop the script if there's no URL to grab
                if not (my URL exists) then return false

                -- Path to where the file will be saved
                set HFSPath to the path of sys's item (my path)
                -- Dereferencing the URL
                set www to my URL as text
                -- Extract the filename portion of the URL
                set filename to the last text item of www

                -- The shell script to grab the contents of a URL
                set sh to the contents of {¬
                "cd", quoted form of the POSIX path of HFSPath, ";", ¬
                "curl --remote-name", ¬
                "--url", quoted form of www} as text


                ## 1. Download the file
                try
                using terms from scripting additions
                do shell script sh
                end using terms from
                on error E
                return E
                end try


                ## 2. Reveal the downloaded file in Finder
                tell application "Finder"
                tell the file named filename in the ¬
                folder named HFSPath to if ¬
                it exists then reveal it
                activate
                end tell
                end run


                It's a longer script than your present one, but most of it is declarations of variables (and properties), after which the script does two simple things:




                1. Grabs the URL of the active tab in Chrome, and downloads the contents of that URL into the specified folder, retaining the same filename and extension as the remote file;


                2. Once the download is complete, it reveals the file in Finder.







                share|improve this answer













                My approach to this problem was to try and avoid scripting the UI if possible, which can be problematic and unreliable. Instead, I decided to use the shell command curl to do the downloading job for us instead of trying to manipulate Chrome into doing it.



                All we need is the location of where to save the file to, which I've set as the location to which Google Chrome defaults to, namely ~/Downloads.



                property path : "~/Downloads" -- Where to download the file to

                use Chrome : application "Google Chrome"
                property sys : application "System Events"

                property window : a reference to window 1 of Chrome
                property tab : a reference to active tab of my window
                property URL : a reference to URL of my tab

                property text item delimiters : {space, "/"}

                on run
                -- Stop the script if there's no URL to grab
                if not (my URL exists) then return false

                -- Path to where the file will be saved
                set HFSPath to the path of sys's item (my path)
                -- Dereferencing the URL
                set www to my URL as text
                -- Extract the filename portion of the URL
                set filename to the last text item of www

                -- The shell script to grab the contents of a URL
                set sh to the contents of {¬
                "cd", quoted form of the POSIX path of HFSPath, ";", ¬
                "curl --remote-name", ¬
                "--url", quoted form of www} as text


                ## 1. Download the file
                try
                using terms from scripting additions
                do shell script sh
                end using terms from
                on error E
                return E
                end try


                ## 2. Reveal the downloaded file in Finder
                tell application "Finder"
                tell the file named filename in the ¬
                folder named HFSPath to if ¬
                it exists then reveal it
                activate
                end tell
                end run


                It's a longer script than your present one, but most of it is declarations of variables (and properties), after which the script does two simple things:




                1. Grabs the URL of the active tab in Chrome, and downloads the contents of that URL into the specified folder, retaining the same filename and extension as the remote file;


                2. Once the download is complete, it reveals the file in Finder.








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 13 '18 at 19:00









                CJKCJK

                2,5781216




                2,5781216






























                    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%2f53284192%2fsaving-a-page-and-clicking-on-the-save-popup-in-chrome-using-applescript%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







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python