Bring application to front by its path











up vote
1
down vote

favorite












How can I bring an application to front by its path?



i.e. Assume /Applications/MyApp.app have already started; At a time I want to bring that MyApp.app window to front by passing its path to AppleScript: myApplScript.scpt /Applications/MyApp.app.



I tried by this script, but this did not worked for me:



on run argv
set apppath to (item 1 of argv) as string
tell application "System Events"
set frontmost of every process whose path is apppath to true
end tell
end run


Thanks!










share|improve this question




























    up vote
    1
    down vote

    favorite












    How can I bring an application to front by its path?



    i.e. Assume /Applications/MyApp.app have already started; At a time I want to bring that MyApp.app window to front by passing its path to AppleScript: myApplScript.scpt /Applications/MyApp.app.



    I tried by this script, but this did not worked for me:



    on run argv
    set apppath to (item 1 of argv) as string
    tell application "System Events"
    set frontmost of every process whose path is apppath to true
    end tell
    end run


    Thanks!










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      How can I bring an application to front by its path?



      i.e. Assume /Applications/MyApp.app have already started; At a time I want to bring that MyApp.app window to front by passing its path to AppleScript: myApplScript.scpt /Applications/MyApp.app.



      I tried by this script, but this did not worked for me:



      on run argv
      set apppath to (item 1 of argv) as string
      tell application "System Events"
      set frontmost of every process whose path is apppath to true
      end tell
      end run


      Thanks!










      share|improve this question















      How can I bring an application to front by its path?



      i.e. Assume /Applications/MyApp.app have already started; At a time I want to bring that MyApp.app window to front by passing its path to AppleScript: myApplScript.scpt /Applications/MyApp.app.



      I tried by this script, but this did not worked for me:



      on run argv
      set apppath to (item 1 of argv) as string
      tell application "System Events"
      set frontmost of every process whose path is apppath to true
      end tell
      end run


      Thanks!







      macos applescript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 10:41

























      asked Nov 7 at 10:22









      Santanu Karar

      505517




      505517
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Usually, all you need to do is to activate the application, which switches focus to it (even if the application is already running):



          activate application "MyApp"


          You can use this command directly with its path like this:



          activate application "/Applications/MyApp.app"


          or, in your specific case,



          activate application apppath


          although you shouldn't need to.



          If that doesn't work, you can try System Events:



          tell application "System Events" to set frontmost of process "MyApp" to true


          or, using its path:



          tell application "System Events" to set the frontmost of the first process ¬
          whose POSIX path of application file is "/Applications/MyApp.app" to true





          share|improve this answer























          • May be worth mentioning that to actually pass the path argument to the AppleScript via the command line run: $ osascript /path/to/myApplScript.scpt /Applications/myApp.app - well that's what I assume the OP means by saying "by passing its path to AppleScript".
            – RobC
            Nov 7 at 14:10










          • However, my assumption of why the OP actually wants to use an AppleScript to bring an app to the front by it's path may be incorrect. As running open /Applications/myApp.app/ via the command line will achieve the same result.
            – RobC
            Nov 7 at 14:21










          • Thanks @CJK, the last one seems best fit for our needs and worked-out good! Thank you! On RobC's query, we ran/trigger 3rd party applications from our custom macOS application, sometime - a few triggered application turns to background (in display order) thus we execute an AppleScript passing the application's path and makes it bring to front (in display order).
            – Santanu Karar
            Nov 8 at 7:01










          • Sorry I forgot this ) ty
            – Santanu Karar
            Nov 8 at 7:08


















          up vote
          -1
          down vote













          Have you tried checking for the open files for the process?
          There are several ways of doing this (see: this article)



          Normally the application binary will be an open file of the process.
          Keep in mind that the binary executable may be contained in a package and you will need to look for it by doing a show package contents in finder and then appending the path to the executable in the package. In the package check under /Contents/MacOs/.



          This might take a long time if you have many open processes and therefore not very practical to run often.
          Also take into account that a single executable file can be running in more then one process instance.



          A pity that the standard Activity Monitor is not scriptable with applescript which I just verified hoping for a more applescript friendly solution. You should still be able to achieve the same result via calling the shell command line.



          Also often the process name is equal or similar to the filename but that depends on the application or process you're looking for.



          I'm guessing you must have different versions of the same app installed that you need to check for the application by pathname?



          Or if you don't need to figure out the path name dynamically you can simply look it up in advance using the Activity Monitor or on the command line. There you can identify the process name that corresponds to you your running application that was started from the specified path. Knowing the process name in advance then makes it easy and you can simply use



          tell application "System Events" to get application process "Name of My Application"

          --insert the actions you want to perform on your running app here

          end tell


          To solve any process uniqueness issues in case the same app is running more then once you can always result to the pid or unix process Id. Here's an article on how to get the pid in applescript of a running app






          share|improve this answer





















          • I wanted to ignore 'name' based solution and that is why I asked this question ) But thank you for your thoughts!
            – Santanu Karar
            Nov 8 at 7:03











          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',
          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%2f53187517%2fbring-application-to-front-by-its-path%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








          up vote
          2
          down vote



          accepted










          Usually, all you need to do is to activate the application, which switches focus to it (even if the application is already running):



          activate application "MyApp"


          You can use this command directly with its path like this:



          activate application "/Applications/MyApp.app"


          or, in your specific case,



          activate application apppath


          although you shouldn't need to.



          If that doesn't work, you can try System Events:



          tell application "System Events" to set frontmost of process "MyApp" to true


          or, using its path:



          tell application "System Events" to set the frontmost of the first process ¬
          whose POSIX path of application file is "/Applications/MyApp.app" to true





          share|improve this answer























          • May be worth mentioning that to actually pass the path argument to the AppleScript via the command line run: $ osascript /path/to/myApplScript.scpt /Applications/myApp.app - well that's what I assume the OP means by saying "by passing its path to AppleScript".
            – RobC
            Nov 7 at 14:10










          • However, my assumption of why the OP actually wants to use an AppleScript to bring an app to the front by it's path may be incorrect. As running open /Applications/myApp.app/ via the command line will achieve the same result.
            – RobC
            Nov 7 at 14:21










          • Thanks @CJK, the last one seems best fit for our needs and worked-out good! Thank you! On RobC's query, we ran/trigger 3rd party applications from our custom macOS application, sometime - a few triggered application turns to background (in display order) thus we execute an AppleScript passing the application's path and makes it bring to front (in display order).
            – Santanu Karar
            Nov 8 at 7:01










          • Sorry I forgot this ) ty
            – Santanu Karar
            Nov 8 at 7:08















          up vote
          2
          down vote



          accepted










          Usually, all you need to do is to activate the application, which switches focus to it (even if the application is already running):



          activate application "MyApp"


          You can use this command directly with its path like this:



          activate application "/Applications/MyApp.app"


          or, in your specific case,



          activate application apppath


          although you shouldn't need to.



          If that doesn't work, you can try System Events:



          tell application "System Events" to set frontmost of process "MyApp" to true


          or, using its path:



          tell application "System Events" to set the frontmost of the first process ¬
          whose POSIX path of application file is "/Applications/MyApp.app" to true





          share|improve this answer























          • May be worth mentioning that to actually pass the path argument to the AppleScript via the command line run: $ osascript /path/to/myApplScript.scpt /Applications/myApp.app - well that's what I assume the OP means by saying "by passing its path to AppleScript".
            – RobC
            Nov 7 at 14:10










          • However, my assumption of why the OP actually wants to use an AppleScript to bring an app to the front by it's path may be incorrect. As running open /Applications/myApp.app/ via the command line will achieve the same result.
            – RobC
            Nov 7 at 14:21










          • Thanks @CJK, the last one seems best fit for our needs and worked-out good! Thank you! On RobC's query, we ran/trigger 3rd party applications from our custom macOS application, sometime - a few triggered application turns to background (in display order) thus we execute an AppleScript passing the application's path and makes it bring to front (in display order).
            – Santanu Karar
            Nov 8 at 7:01










          • Sorry I forgot this ) ty
            – Santanu Karar
            Nov 8 at 7:08













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          Usually, all you need to do is to activate the application, which switches focus to it (even if the application is already running):



          activate application "MyApp"


          You can use this command directly with its path like this:



          activate application "/Applications/MyApp.app"


          or, in your specific case,



          activate application apppath


          although you shouldn't need to.



          If that doesn't work, you can try System Events:



          tell application "System Events" to set frontmost of process "MyApp" to true


          or, using its path:



          tell application "System Events" to set the frontmost of the first process ¬
          whose POSIX path of application file is "/Applications/MyApp.app" to true





          share|improve this answer














          Usually, all you need to do is to activate the application, which switches focus to it (even if the application is already running):



          activate application "MyApp"


          You can use this command directly with its path like this:



          activate application "/Applications/MyApp.app"


          or, in your specific case,



          activate application apppath


          although you shouldn't need to.



          If that doesn't work, you can try System Events:



          tell application "System Events" to set frontmost of process "MyApp" to true


          or, using its path:



          tell application "System Events" to set the frontmost of the first process ¬
          whose POSIX path of application file is "/Applications/MyApp.app" to true






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 7 at 11:16

























          answered Nov 7 at 11:09









          CJK

          2,3471114




          2,3471114












          • May be worth mentioning that to actually pass the path argument to the AppleScript via the command line run: $ osascript /path/to/myApplScript.scpt /Applications/myApp.app - well that's what I assume the OP means by saying "by passing its path to AppleScript".
            – RobC
            Nov 7 at 14:10










          • However, my assumption of why the OP actually wants to use an AppleScript to bring an app to the front by it's path may be incorrect. As running open /Applications/myApp.app/ via the command line will achieve the same result.
            – RobC
            Nov 7 at 14:21










          • Thanks @CJK, the last one seems best fit for our needs and worked-out good! Thank you! On RobC's query, we ran/trigger 3rd party applications from our custom macOS application, sometime - a few triggered application turns to background (in display order) thus we execute an AppleScript passing the application's path and makes it bring to front (in display order).
            – Santanu Karar
            Nov 8 at 7:01










          • Sorry I forgot this ) ty
            – Santanu Karar
            Nov 8 at 7:08


















          • May be worth mentioning that to actually pass the path argument to the AppleScript via the command line run: $ osascript /path/to/myApplScript.scpt /Applications/myApp.app - well that's what I assume the OP means by saying "by passing its path to AppleScript".
            – RobC
            Nov 7 at 14:10










          • However, my assumption of why the OP actually wants to use an AppleScript to bring an app to the front by it's path may be incorrect. As running open /Applications/myApp.app/ via the command line will achieve the same result.
            – RobC
            Nov 7 at 14:21










          • Thanks @CJK, the last one seems best fit for our needs and worked-out good! Thank you! On RobC's query, we ran/trigger 3rd party applications from our custom macOS application, sometime - a few triggered application turns to background (in display order) thus we execute an AppleScript passing the application's path and makes it bring to front (in display order).
            – Santanu Karar
            Nov 8 at 7:01










          • Sorry I forgot this ) ty
            – Santanu Karar
            Nov 8 at 7:08
















          May be worth mentioning that to actually pass the path argument to the AppleScript via the command line run: $ osascript /path/to/myApplScript.scpt /Applications/myApp.app - well that's what I assume the OP means by saying "by passing its path to AppleScript".
          – RobC
          Nov 7 at 14:10




          May be worth mentioning that to actually pass the path argument to the AppleScript via the command line run: $ osascript /path/to/myApplScript.scpt /Applications/myApp.app - well that's what I assume the OP means by saying "by passing its path to AppleScript".
          – RobC
          Nov 7 at 14:10












          However, my assumption of why the OP actually wants to use an AppleScript to bring an app to the front by it's path may be incorrect. As running open /Applications/myApp.app/ via the command line will achieve the same result.
          – RobC
          Nov 7 at 14:21




          However, my assumption of why the OP actually wants to use an AppleScript to bring an app to the front by it's path may be incorrect. As running open /Applications/myApp.app/ via the command line will achieve the same result.
          – RobC
          Nov 7 at 14:21












          Thanks @CJK, the last one seems best fit for our needs and worked-out good! Thank you! On RobC's query, we ran/trigger 3rd party applications from our custom macOS application, sometime - a few triggered application turns to background (in display order) thus we execute an AppleScript passing the application's path and makes it bring to front (in display order).
          – Santanu Karar
          Nov 8 at 7:01




          Thanks @CJK, the last one seems best fit for our needs and worked-out good! Thank you! On RobC's query, we ran/trigger 3rd party applications from our custom macOS application, sometime - a few triggered application turns to background (in display order) thus we execute an AppleScript passing the application's path and makes it bring to front (in display order).
          – Santanu Karar
          Nov 8 at 7:01












          Sorry I forgot this ) ty
          – Santanu Karar
          Nov 8 at 7:08




          Sorry I forgot this ) ty
          – Santanu Karar
          Nov 8 at 7:08












          up vote
          -1
          down vote













          Have you tried checking for the open files for the process?
          There are several ways of doing this (see: this article)



          Normally the application binary will be an open file of the process.
          Keep in mind that the binary executable may be contained in a package and you will need to look for it by doing a show package contents in finder and then appending the path to the executable in the package. In the package check under /Contents/MacOs/.



          This might take a long time if you have many open processes and therefore not very practical to run often.
          Also take into account that a single executable file can be running in more then one process instance.



          A pity that the standard Activity Monitor is not scriptable with applescript which I just verified hoping for a more applescript friendly solution. You should still be able to achieve the same result via calling the shell command line.



          Also often the process name is equal or similar to the filename but that depends on the application or process you're looking for.



          I'm guessing you must have different versions of the same app installed that you need to check for the application by pathname?



          Or if you don't need to figure out the path name dynamically you can simply look it up in advance using the Activity Monitor or on the command line. There you can identify the process name that corresponds to you your running application that was started from the specified path. Knowing the process name in advance then makes it easy and you can simply use



          tell application "System Events" to get application process "Name of My Application"

          --insert the actions you want to perform on your running app here

          end tell


          To solve any process uniqueness issues in case the same app is running more then once you can always result to the pid or unix process Id. Here's an article on how to get the pid in applescript of a running app






          share|improve this answer





















          • I wanted to ignore 'name' based solution and that is why I asked this question ) But thank you for your thoughts!
            – Santanu Karar
            Nov 8 at 7:03















          up vote
          -1
          down vote













          Have you tried checking for the open files for the process?
          There are several ways of doing this (see: this article)



          Normally the application binary will be an open file of the process.
          Keep in mind that the binary executable may be contained in a package and you will need to look for it by doing a show package contents in finder and then appending the path to the executable in the package. In the package check under /Contents/MacOs/.



          This might take a long time if you have many open processes and therefore not very practical to run often.
          Also take into account that a single executable file can be running in more then one process instance.



          A pity that the standard Activity Monitor is not scriptable with applescript which I just verified hoping for a more applescript friendly solution. You should still be able to achieve the same result via calling the shell command line.



          Also often the process name is equal or similar to the filename but that depends on the application or process you're looking for.



          I'm guessing you must have different versions of the same app installed that you need to check for the application by pathname?



          Or if you don't need to figure out the path name dynamically you can simply look it up in advance using the Activity Monitor or on the command line. There you can identify the process name that corresponds to you your running application that was started from the specified path. Knowing the process name in advance then makes it easy and you can simply use



          tell application "System Events" to get application process "Name of My Application"

          --insert the actions you want to perform on your running app here

          end tell


          To solve any process uniqueness issues in case the same app is running more then once you can always result to the pid or unix process Id. Here's an article on how to get the pid in applescript of a running app






          share|improve this answer





















          • I wanted to ignore 'name' based solution and that is why I asked this question ) But thank you for your thoughts!
            – Santanu Karar
            Nov 8 at 7:03













          up vote
          -1
          down vote










          up vote
          -1
          down vote









          Have you tried checking for the open files for the process?
          There are several ways of doing this (see: this article)



          Normally the application binary will be an open file of the process.
          Keep in mind that the binary executable may be contained in a package and you will need to look for it by doing a show package contents in finder and then appending the path to the executable in the package. In the package check under /Contents/MacOs/.



          This might take a long time if you have many open processes and therefore not very practical to run often.
          Also take into account that a single executable file can be running in more then one process instance.



          A pity that the standard Activity Monitor is not scriptable with applescript which I just verified hoping for a more applescript friendly solution. You should still be able to achieve the same result via calling the shell command line.



          Also often the process name is equal or similar to the filename but that depends on the application or process you're looking for.



          I'm guessing you must have different versions of the same app installed that you need to check for the application by pathname?



          Or if you don't need to figure out the path name dynamically you can simply look it up in advance using the Activity Monitor or on the command line. There you can identify the process name that corresponds to you your running application that was started from the specified path. Knowing the process name in advance then makes it easy and you can simply use



          tell application "System Events" to get application process "Name of My Application"

          --insert the actions you want to perform on your running app here

          end tell


          To solve any process uniqueness issues in case the same app is running more then once you can always result to the pid or unix process Id. Here's an article on how to get the pid in applescript of a running app






          share|improve this answer












          Have you tried checking for the open files for the process?
          There are several ways of doing this (see: this article)



          Normally the application binary will be an open file of the process.
          Keep in mind that the binary executable may be contained in a package and you will need to look for it by doing a show package contents in finder and then appending the path to the executable in the package. In the package check under /Contents/MacOs/.



          This might take a long time if you have many open processes and therefore not very practical to run often.
          Also take into account that a single executable file can be running in more then one process instance.



          A pity that the standard Activity Monitor is not scriptable with applescript which I just verified hoping for a more applescript friendly solution. You should still be able to achieve the same result via calling the shell command line.



          Also often the process name is equal or similar to the filename but that depends on the application or process you're looking for.



          I'm guessing you must have different versions of the same app installed that you need to check for the application by pathname?



          Or if you don't need to figure out the path name dynamically you can simply look it up in advance using the Activity Monitor or on the command line. There you can identify the process name that corresponds to you your running application that was started from the specified path. Knowing the process name in advance then makes it easy and you can simply use



          tell application "System Events" to get application process "Name of My Application"

          --insert the actions you want to perform on your running app here

          end tell


          To solve any process uniqueness issues in case the same app is running more then once you can always result to the pid or unix process Id. Here's an article on how to get the pid in applescript of a running app







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 7 at 19:06









          stepvda

          246




          246












          • I wanted to ignore 'name' based solution and that is why I asked this question ) But thank you for your thoughts!
            – Santanu Karar
            Nov 8 at 7:03


















          • I wanted to ignore 'name' based solution and that is why I asked this question ) But thank you for your thoughts!
            – Santanu Karar
            Nov 8 at 7:03
















          I wanted to ignore 'name' based solution and that is why I asked this question ) But thank you for your thoughts!
          – Santanu Karar
          Nov 8 at 7:03




          I wanted to ignore 'name' based solution and that is why I asked this question ) But thank you for your thoughts!
          – Santanu Karar
          Nov 8 at 7:03


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53187517%2fbring-application-to-front-by-its-path%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







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()