title: AppleScript: how to check if folders/files starting with a string exist?
body: <p>I am trying to check if any directory exists in a folder that starts with &quot;A&quot; and then list all these folders into a variable.</p>
  <p>I must do an &quot;if exists ...&quot; check at first, otherwise I get an error when setting the variable if no folder exists starting with &quot;A&quot; in the given directory.</p>
  <p>Here is my AppleScript code:</p>
  <pre><code>property libraryPath : (path to library folder from user domain) as string
  
  tell application &quot;Finder&quot;
      if exists (folder in folder libraryPath whose name begins with &quot;A&quot;) then
          set folderlist to name of every folder in folder libraryPath whose name begins with &quot;A&quot;
      else
          set folderlist to {}
      end if
      
      get name of every folder in folder libraryPath whose name begins with &quot;A&quot;
  end tell
  </code></pre>
  <p>The results are:</p>
  <pre><code>tell application &quot;Finder&quot;
      exists folder of folder &quot;Mojave EXT:Users:devtest:Library:&quot; whose name starts with &quot;A&quot;
          --&gt; false
  
      get name of every folder of folder &quot;Mojave EXT:Users:devtest:Library:&quot; whose name starts with &quot;A&quot;
          --&gt; {&quot;Accounts&quot;, &quot;Application Scripts&quot;, &quot;Application Support&quot;, &quot;Assistant&quot;, &quot;Assistants&quot;, &quot;Audio&quot;, &quot;Autosave Information&quot;}
  end tell
  </code></pre>
  <p>As you see, the strange thing is that I got <code>false</code> as the result for checking the existence of folders starting with &quot;A&quot;, but then again I can get all the existing folders right after it.</p>
  <p>Why? What do I do wrong?</p>
  <p>Thank you in advance!</p>