Search code examples
escapingapplescriptquotes

Use string variable as Applescript command argument


I'm trying to do the following:

on cleanup(x)
   tell application "Finder"    
      clean up window 1 by x
   end tell
end cleanup

cleanup("name")

However since x variable is a string, the clean up command doesn't accept it and exits with error. Is there a way to convert the string to something that the commands accepts or some other solution to unquote the variable without using if, else statements like this:

on cleanup(x)
   tell application "Finder"
      if x is "name" then
          clean up window 1 by name
      end if
   end tell
end cleanup

cleanup("name")

Solution

  • This should work.

        on cleanup(x)
    
        run script "tell application \"Finder\" to  clean up window 1 by " & space & x
    
    
    
    end cleanup
    
    cleanup("name")
    

    From the StandardAdditions Library

    run script v : Run a specified script or script file

    • run script script : the script text (or an alias or file reference to a script file) to run

      • [with parameters list of any] : a list of parameters

      • [in text] : the scripting component to use; default is the current scripting component

    → any : the result of running the script