How to make a script to open a file/folder ?

Posted:
in Mac Software edited January 2014
I'm new to scripting in OS X. How can I build a simple script to tell the Finder to open a given folder, and to put the window in front, using Automator ?

Comments

  • Reply 1 of 8
    MarvinMarvin Posts: 15,310moderator
    In the command line, it's as simple as:



    open "/path/to/folder/or/file.jpg"



    So any scripting environment that can run a shell script can execute that with a system call. How do you plan to enter the pathname of the item to open?
  • Reply 2 of 8
    kalikali Posts: 634member
    Thanks for the reply.



    As for the path, I'm not sure yet. Also, the script should bring the folder or the file to front.



    EDIT : Currently, I don't understand how Automator is working. All I want to do is to create a small app that open a specific text file with TextEdit. How am I supposed to do that ?
  • Reply 3 of 8
    kalikali Posts: 634member
    Okay, I'm beginning to understand now. My first script-app is working exactly as I wanted to.
  • Reply 4 of 8
    kalikali Posts: 634member
    Well, I now have a problem and I don't know how to solve it :



    The script is very simple and just bring the Finder to front and open a folder :



    Quote:

    tell application "Finder"

    \tactivate

    \topen POSIX file "/Users/userName/Applications/aFolder"

    end tell




    I saved the script as an application. When I double-click on it in the Finder, it's doing exactly what I want it to do. However, if I launch the same script-app from another application, the script brings a window to ask the user to run the script or to quit it. This, I don't want . The script should just run without asking anything, like what it already do when directly launched from the Finder.



    How can I remove the modal dialog box ?
  • Reply 5 of 8
    MarvinMarvin Posts: 15,310moderator
    Did you add this as a service or to an Applescript menu?



    This app can help you make a service if you can't just drop the app in a services folder:



    http://wafflesoftware.net/thisservice/
  • Reply 6 of 8
    kalikali Posts: 634member
    I tried to add a service, but it doesn't work at all (the service do nothing, while the script itself appears to be okay). I don't understand what is going on.



    Geez, why is it so complicated just to open a folder using a keyboard shortcut ? The script is just this :



    Quote:

    tell application "Finder"

    \tactivate

    \topen POSIX file "/Users/name/Applications/folder"

    end tell



  • Reply 7 of 8
    MarvinMarvin Posts: 15,310moderator
    Quote:
    Originally Posted by Kali View Post


    I tried to add a service, but it doesn't work at all (the service do nothing, while the script itself appears to be okay). I don't understand what is going on.



    Geez, why is it so complicated just to open a folder using a keyboard shortcut ? The script is just this :



    It seems like that above service packager is incompatible with Snow Leopard as the service crashes when running. You can use Automator though. Start with a service template. Set it to receive no input and then add an Applescript to the workflow panel. Then save and it will save direct to the services folder for you.



    A better option might be to use a 3rd party launcher though as the services menu doesn't show up in all apps like Calculator for example. If you used Quicksilver and bound an Applescript to a command, it would launch from anywhere. Application scripting and key-bindings are something I think Apple really need to improve because they are so useful.



    Applescript + Automator + Services just throws more underpowered tools on an already complex feature. The amount of uses it has are few because it still needs programming to use it effectively so absolute novices won't go near it and it's too underpowered for proper developers so you're left with that middle ground and I just don't see many people picking it up.



    I think a better option would be to put the scripting menu in the Apple menu so it is always present. Then scrap Applescript. It takes 10 times more code to do something than Python or Ruby and those languages make more sense and are more useful in the long term. Apple have a Python binding for Applescript now anyway.



    Applescript doesn't work well internationally because the program delimiters are in English instead of characters.
  • Reply 8 of 8
    kalikali Posts: 634member
    I fully agree with you, Marvin. I always though that Apple's way to keyboard shortcuts is ackward, unatural and ineffective. I don't understand how and why they totally scraped it, since we already got some good shareware systems to do simple and efficient systemwide keyboard shortcuts.



    Thanks alot for your time and help.
Sign In or Register to comment.