10.3 C
New York
Thursday, March 6, 2025

Convert an if-condition from Automator-powered AppleScript to pure AppleScript


Right here is an AppleScript script to make it doable double-click information in Finder in order that they are going to be opened in Vim, by Normen Hansen: https://github.com/normen/vim-macos-scripts/blob/grasp/open-file-in-vim.applescript (it should be used from Automator).

I attempt to simplify it and in addition to make it doable to make use of it with out Automator. To make use of my very own model, you solely want to put it aside as utility as a substitute, like this:

osacompile -o open-with-vim.app open-with-vim.applescript

after which copy it to /Purposes and set your .txt information to be opened utilizing this app.

Right here it’s, it alsmost works:

-- https://github.com/normen/vim-macos-scripts/blob/grasp/open-file-in-vim.applescript
-- opens Vim with file or file listing
-- units present folder to guardian folder of first file

on open theFiles
  set command to {}
  if enter is null or enter is {} or ((merchandise 1 in enter) as string) is "" then
    -- no information, open vim with out parameters
    set finish of command to "vim;exit"
  else
    set firstFile to (merchandise 1 of theFiles) as string
    inform utility "Finder" to set pathFolderParent to quoted type of (POSIX path of ((folder of merchandise firstFile) as string))
    set finish of command to "cd" & house & (pathFolderParent as string)
    set finish of command to ";hx" -- e.g. vi or some other command-line textual content editor
    set fileList to {}
    repeat with i from 1 to (theFiles rely)
      set finish of fileList to house & quoted type of (POSIX path of (merchandise i of theFiles as string))
    finish repeat
    set finish of command to (fileList as string)
    set finish of command to ";exit" -- if Terminal > Settings > Profiles > Shell > When the shell exits != Do not shut the window
  finish if
  set command to command as string
  set myTab to null
  inform utility "Terminal"
    if it's not operating then
      set myTab to (do script command in window 1)
    else
      set myTab to (do script command)
    finish if
    activate
  finish inform
  return
finish open

The one drawback is the if block within the very starting:

if enter is null or enter is {} or ((merchandise 1 in enter) as string) is "" then

What’s unsuitable there? Learn how to make it work? (Assuming it already works wonderful if AppleScript is used utilizing Automator.)

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles