12 Replies Latest reply: Jan 9, 2013 5:19 PM by MacOSXNoob
MacOSXNoob Level 1 Level 1 (5 points)

I am fairly familiar with AppleScript, but I just downloaded Xcode, and I am trying to make a simple app, but I am completely lost.

What I am making is an app where it asks for icons for Finder, Empty Trash, and Full Trash. I have three check boxes, "Finder," "Trash Empty," and "Trash Full." Whichever are checked, when you click the "Change my Icons!" button, it will ask for the appropriate icons, and move them to: /System/Library/CoreServices/Dock.app/Contents/Resources, then run the Terminal command: killall Dock. I have the appearance set up, but I am clueless on how to match the buttons to the command, and how to make a variable out of each check mark.

 

Thanks So much!


MacBook Pro, OS X Mountain Lion (10.8.2)
  • 1. Re: AppleScriptObjC  app with check marks, asking for files, and moving the files?
    Frank Caggiano Level 7 Level 7 (23,850 points)

    That's a lot to try and take in in one shot. Xcode ifself will take you a while to get comfortable with if you've never used it before.

     

    There are some good tutorials out there and a few books worth getting. Plus Apples documentation is pretty good on this.

     

    Here is a link to a tutorial http://macscripter.net/viewtopic.php?id=30274 that should get you started. It's sort of an Hello World app. It will show you how to attach GUI elements and Applescript objects.

     

    One thing to be aware of. Xcode is a real moving target. A lot of sites and books will be out of date so keep that in mind if something you're trying isn;t working like the site or book days it should.

     

    good luck

  • 2. Re: AppleScriptObjC  app with check marks, asking for files, and moving the files?
    Frank Caggiano Level 7 Level 7 (23,850 points)

    Looking over that tutorial it is a bit out of date but the main points it is doing still hold. Specifically it looks like the Interface builder is a  older version then what you have. Now it all opens in one app it use to be multiple apps (xcode editor and interface builder were separate, now they are one).

     

    One book that really help me get started is Learn AppleScript by Apress Not only is it a great AppleScript tutorial and reference but it has a great section on getting started with AppleScript and Objective-C

     

    good luck

  • 3. Re: AppleScriptObjC  app with check marks, asking for files, and moving the files?
    MacOSXNoob Level 1 Level 1 (5 points)

    So... would this work:

    on ChangeFullTrashIconPushed_(sender)

            set FullTrashIcon to (choose file with prompt "Choose a PNG icon to use for your Full Trash icon:" of type {"PNG"} without invisibles)

    set theFile to "Macintosh HD:System:Library:CoreServices:Dock.app:Contents:Resources:trashfull.png"

    set qpp to quoted form of POSIX path of theFile

    do shell script "zip " & qpp & ".zip " & qpp

            tell application "Finder"

                set theDupe2 to duplicate FullTrashIcon

                set the name of theDupe2 to "trashfull.png"

                move theDupe2 to "Macintosh HD:System:Library:CoreServices:Dock.app:Contents:Resources" with replacing

            end tell

            do shell script "killall Dock"

        end ChangeFullTrashIconPushed_

     

    then I control-dragged a button to that script, and did a similar thing with buttons "Change Finder Icon" and "Change Empty Trash Icon"  Now I have a didderent problem. When I try to run it, I get this error:

    Command /usr/bin/osacompile failed with exit code 1

    What does that mean? I sort of messed up with creating a new project of the same thing and not replacing the old one due to a misspelling, so I renamed it in Xcode, and I don't know if that is related, but I also heard in reviews that simulator doesn't work well. How do I fix it? and did I do the first part right?

     

    Thanks so much

  • 4. Re: AppleScriptObjC  app with check marks, asking for files, and moving the files?
    Frank Caggiano Level 7 Level 7 (23,850 points)

    With Xcode the way the connections are made and how the property tags are wrriten are very important it is hard to look at a piece of code like you posted and coment on it one way or the other.

     

    Two things, the compile error should have a line number telling you where the error occured. Turn on show line numbers for the code editor and you'll at least know where the error is happening. The part of the message you posted tells us nothing.

     

    Second I'd really suggest you start with soemthing simpler. Do the tutorial I linked to above to at least get a feel for how all this go together. Just a button and a text field to see ow the connections are made is a good  place to start.

     

    I can say that

    on ChangeFullTrashIconPushed_(sender)

    appears correct as far as it goes. But how it is declared and how it is conected to the window are just as important.

     

    If you want to use the code you wrote above then trash everything between the

    on ChangeFullTrashIconPushed_(sender)

    and

    end

     

    and replace it with

    display dialog "MY button was pushed"

     

    and get this to work. Once you have that you at least know your GUI is working then you can add the other code back in.

  • 5. Re: AppleScriptObjC  app with check marks, asking for files, and moving the files?
    MacOSXNoob Level 1 Level 1 (5 points)

    I delete the old ones and started fresh. Now, I have a script problem.

    when I run the script I showed before, but without the compressing part (I decided to add a "Restore" button instead of archiving), I get the message: Finder got an error: Can’t get document file "1357712224_Finder copy.png" of folder "Downloads" of folder "milodarling" of folder "Users" of startup disk.

    I understand that this is because it is looking for the original name of theDupe2, which no longer exists because the script renamed it. How do I move the newly named file to the desired folder?

     

    Thank you

  • 6. Re: AppleScriptObjC  app with check marks, asking for files, and moving the files?
    Frank Caggiano Level 7 Level 7 (23,850 points)

    If you have the Xcode/Objective-C Applescript part worked out I woul suggest you satrt a need thread dealing soely with any Applescript probelms you are having.

     

    regards

  • 7. Re: AppleScriptObjC  app with check marks, asking for files, and moving the files?
    red_menace Level 6 Level 6 (14,615 points)

    If you use an alias, the reference remains valid even if you move or rename the file item.

  • 8. Re: AppleScriptObjC  app with check marks, asking for files, and moving the files?
    MacOSXNoob Level 1 Level 1 (5 points)

    How do I make this duplicate variable an alias? I cannot think of another way to identify it.

    Also, how do I access the Resources folder in applescript with Xcode? The normal applescript I've used in applescript apps:

     

    set OriginalFinderIcon to ((path to me as text) & "Contents:Resources:finder.png")

    tell application "Finder"

    duplicate OriginalFinderIcon to "Macintosh HD:System:Library:CoreServices:Dock.app:Contents:Resources:" with replacing

    end tell

     

    This script is not working with my Xcode app.

    I have the original finder, trashempty, and trashfull files in there, and would like a button that copies those files to the correct folder when the "Restore _____ Icon" button is pushed

     

    Thanks

  • 9. Re: AppleScriptObjC  app with check marks, asking for files, and moving the files?
    red_menace Level 6 Level 6 (14,615 points)

    To get an alias of the duplicate file, you can just coerce the Finder reference to an alias, for example

         set theDupe2 to (duplicate FullTrashIcon) as alias

     

    You can access items in the Resources folder of the application bundle the regular way:

         set thePath to path to resource "trashfull.png" -- alias

    or the ASObjC way, using the NSBundle class:

         set thePath to current application's NSBundle's mainBundle's pathForResource_ofType_("trashfull", "png") -- POSIX

     

    edit: you can't use path to me in ASObjC, use the above instead.

  • 10. Re: AppleScriptObjC  app with check marks, asking for files, and moving the files?
    MacOSXNoob Level 1 Level 1 (5 points)

    Thank you so much! The resource script worked! But the duplicate is getting another error:

    Can’t make alias "Macintosh HD:Users:milodarling:Downloads:cab copy.png" into type reference.

    "cab copy.png" was the file I chose to test the script. Applescript is having an error duplicating that file.

    What do I do to fix it?

    If this is too difficult, or you can't figure out how to fix it, I can always stick with not duplicating it. The thing is, though, I would prefer to leave the imae that the user chose to be untouched, not to disappear into a system folder where they will likely never find it. If possible, please show me how to fix it.

     

    Your help is very much appreciated

  • 11. Re: AppleScriptObjC  app with check marks, asking for files, and moving the files?
    red_menace Level 6 Level 6 (14,615 points)

    I'm not having any problems duplicating the file.  For testing, you should be able to swap the rename and move statements and it will still work:

     

    set FullTrashIcon to (choose file)

    tell application "Finder"

      set theDupe2 to (duplicate FullTrashIcon) as alias

      set the name of theDupe2 to "trashfull.png"

      move theDupe2 to desktop

    end tell

     

    I'm using the desktop here, but you can also use the temporary items folder or a folder in Application Support.

  • 12. Re: AppleScriptObjC  app with check marks, asking for files, and moving the files?
    MacOSXNoob Level 1 Level 1 (5 points)

    My mistake.

    I didn't put the fixed script line under "tell application "Finder""

    everything works now!

    Thank you so much!

    You are a genius!