Loading ...
Sorry, an error occurred while loading the content.

Opening File and Positioning Cursor on Line from Shell

Expand Messages
  • sbrantschen
    I am aware of see to open files directly from the shell. Now I need two functions that go beyond that, namely: -- opening a file with the cursor positioned
    Message 1 of 3 , Apr 17, 2007
    • 0 Attachment
      I am aware of "see" to open files directly from the shell. Now I need two functions that go
      beyond that, namely:

      -- opening a file with the cursor positioned at a specific line

      -- setting the cursor to a specific line in an already open file window

      As said, from the command line. I guess it can be done with some "osascript" wizardry, but
      alas I am not really fluent in AppleScript (to say the least :.-). Could some good soul with the
      right knowledge give me some support, please?

      Thanks a lot.
      -- Stefan
    • Ben Gollmer
      ... I was able to accomplish this with a combination of AppleScript and bash. First, I put this script in /usr/local/bin/seeline (it can go anywhere in your
      Message 2 of 3 , Apr 18, 2007
      • 0 Attachment
        On Apr 17, 2007, at 1:12 PM, sbrantschen wrote:

        > I am aware of "see" to open files directly from the shell. Now I
        > need two functions that go
        > beyond that, namely:
        >
        > -- opening a file with the cursor positioned at a specific line
        >
        > -- setting the cursor to a specific line in an already open file
        > window
        >
        > As said, from the command line. I guess it can be done with some
        > "osascript" wizardry, but
        > alas I am not really fluent in AppleScript (to say the least :.-).
        > Could some good soul with the
        > right knowledge give me some support, please?

        I was able to accomplish this with a combination of AppleScript and
        bash. First, I put this script in /usr/local/bin/seeline (it can go
        anywhere in your $PATH, make sure to chmod +x it):

        #!/bin/bash
        # Opens SubEthaEdit with a particular file and runs an AppleScript to
        # set the cursor to the given line.
        FP=`echo $1`
        see $FP
        osascript ~/Library/Scripts/seeline.applescript $FP $2

        Then, I put this AppleScript in ~/Library/Scripts/seeline.applescript:

        (* AppleScript to set the cursor of a SubEthaEdit document to
        * a particular line. Requires the full path to the file and a
        * line number.
        *)
        on run argv
        try
        if (count of argv) is less than 2 then
        activate application "SubEthaEdit"
        else
        tell application "SubEthaEdit"
        repeat with i from 1 to count of documents
        if path of document i is equal to item 1 of argv then
        set startCharacterIndex of selection of document i to 0
        set length of selection of document i to 0
        set p to paragraph (item 2 of argv as integer) of plain
        text of document i
        set selection of document i to p
        set length of selection of document i to 0
        end if
        end repeat
        activate
        end tell
        end if
        on error the error_message number the error_number
        display dialog "Error " & the error_number & ": " & the
        error_message
        end try
        end run

        Now, try running `seeline /path/to/file linenumber`. If the file is
        already open, the cursor will be moved - otherwise SEE will open it,
        then move the cursor.


        Cheers,
        --
        Ben
      • Stefan Brantschen
        ... Hash: SHA1 Ben ... Thanks. I wasn t aware that osascript can take parameters from the command line. Great. (For this, I was working on a solution with a
        Message 3 of 3 , Apr 18, 2007
        • 0 Attachment
          -----BEGIN PGP SIGNED MESSAGE-----
          Hash: SHA1

          Ben

          On 18 Apr 2007, at 20:54, Ben Gollmer wrote:

          > Now, try running `seeline /path/to/file linenumber`. If the file is
          > already open, the cursor will be moved - otherwise SEE will open
          > it, then move the cursor.

          Thanks. I wasn't aware that osascript can take parameters from the
          command line. Great. (For this, I was working on a solution with a
          "here document" for the applescript and parameter expansion in a bash
          script -- now I checked: argument passing is possible since 10.4,
          though...).

          With regards
          - -- Stefan


          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v1.4.3 (Darwin)

          iQEVAwUBRib5RzqiXtt/m8NcAQLr3ggA0KUrm1hkNGzdfV5WqnLg7xqN/5elxo37
          DSTyk2AsGzgGyqKIQ8Kfa6g8FwxU8sQ3AXRWXxNJ+IEU9pdPBWrYlOS308/IHg6b
          WqZLa28wfcro6SM6JclGSWqtZK93Yecavu9fsnv4VRTdQfE3fii4Pnd4XlNNQ1gL
          JcGm/f6uxJq3PvFHD57ACg9MoLHTaXQ+K0hXqLIacH1BRSqpS1+48TIdm3vX9vbH
          BmAxc83Lyo/f2ZVvg01vO6ISml178+5NsGWaIDhBGBtu0wTeBFnXCZsZqg5y6VuU
          Bcs777BQ2ODPuwTvmbMH2vh/Lp4K3JM/dl6XkHM3XJ1wXsZ84+RNvQ==
          =N6YX
          -----END PGP SIGNATURE-----
        Your message has been successfully submitted and would be delivered to recipients shortly.