1

I am trying to make an Applescript that moves a spotlighted file into a folder specified by a dialog. I would like the script to find the path of the folder that I entered, however, I cannot get it to work. Can anyone help? Thanks in advance and bonus points if you can tell me why my code doesn't all go into one block on my preview :( !!

The problem comes where the asterisks begin and end.

Here is the code.

 repeat 1 times

    set spotlightquery to quoted form of text returned of (display dialog "What do you want to search?" default answer "" buttons {"Search!"} default button 1)

    if spotlightquery = "''" then
        exit repeat
    end if
    set thefolders to {path to home folder}
    set founditems to {}
    repeat with i in thefolders
        set thepath to quoted form of POSIX path of i
        if exists thepath then
            set command to "mdfind -onlyin " & thepath & " " & spotlightquery
            set founditems to founditems & (paragraphs of (do shell script command))
        end if
    end repeat
    if founditems = {} then
        display dialog "No items found." buttons {"OK"} default button 1
        exit repeat
    end if

    display dialog "" & founditems & "" buttons {"OK"} default button 1
    set location to the button returned of (display dialog "Where would you like to move it?" buttons {"C Folder", "Desktop", "Other"} default button 3)
    if location = "C Folder" then
        tell application "Finder"
            set moveTo to "Macintosh HD:Users:aaronmcclellan:Desktop:Coding:C"
            move file founditems to folder moveTo
        end tell
    else
        if location = "Desktop" then
            tell application "Finder"
                set moveTo to the path to desktop folder
                move file founditems to folder moveTo
            end tell
        end if
        *if location = "Other" then
            set fold to the text returned of (display dialog "Where would you like to move it?" default answer "" buttons {"OK"} default button 1)
            set moveTo to fold
            tell application "System Events"
                ##error occurs here
                            move file founditems to folder moveTo
            end tell
        end if*
    end if
  end repeat
8
  • You really should debug first and figure out which line is failing. Put display dialog's at checkpoints and verify the variables are as they should be. Then you can ask why a particular line or block isn't working, instead of this whole routine. Commented Feb 5, 2014 at 4:31
  • I added asterisks where the code starts to fail. Thanks! Commented Feb 5, 2014 at 19:13
  • What line exactly are you getting the error on, and what exactly is the error you are getting? Commented Feb 6, 2014 at 15:18
  • Yes, hard to help otherwise. wrap a Try block around that if block and report the error reported. Other questions: Are C Folder and Desktop options working? Why are you using a DispDialog and not a Choose folder dialog for Other? Are you typing the folder path in old:mac:format or posix/path/format? If posix, you'll need to change your move line to accommodate. Commented Feb 6, 2014 at 17:13
  • Sorry everyone. I have had work to do, so I couldn't help and read your comments. I added where the error occurs. Commented Feb 7, 2014 at 14:46

2 Answers 2

1

You have these two lines... I believe you are wanting these two variables to match?

set getMovedTo to "Macintosh HD:Users:aaronmcclellan:Desktop:Coding:C"
move file founditems to folder moveTo
Sign up to request clarification or add additional context in comments.

1 Comment

You are correct. However, that is an easy fix. Thanks for pointing it out.
1

foundItems is a list of strings, but you're trying to treat it like a file. The move line is evaluated in the following way:

move (file founditems) to (folder moveTo)

Which causes an error because the list foundItems can't be turned into a file.

You need to turn each of the paths into a file individually:

repeat with theFile in foundItems
    move file theFile to folder moveTo
end repeat

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.