title: Activating an Application in Applescript with the Application as a Variable
body: <p>I'm trying to write a script that logs the current app, switches to another app, does some task, and comes back to the original app. This is what I have</p>
  
  <pre><code>set currentApp to my getCurrentApp()
  activate application "Safari"
  # Some task
  activate application currentApp
  
  to getCurrentApp()
  set front_app to (path to frontmost application as Unicode text)
  set AppleScript's text item delimiters to ":"
  set front_app to front_app's text items
  set AppleScript's text item delimiters to {""} --&gt; restore delimiters to default value
  set item_num to (count of front_app) - 1
  set app_name to item item_num of front_app
  set AppleScript's text item delimiters to "."
  set app_name to app_name's text items
  set AppleScript's text item delimiters to {""} --&gt; restore delimiters to default value
  set MyApp to item 1 of app_name
  return MyApp
  end getCurrentApp
  </code></pre>
  
  <p>The weird thing is that the activate application command works if you type in a string literal, but if you pass it a string variable, it will not activate the application. Any ideas why?</p>