I've got a folder reference in Xcode that needs to be configurable by the user. I'd prefer to use a script or plist to set its Location and Full Path properties at build time. I've looked around the web but can't seem to find a way of doing this - is there?
Related
I created this Automator app that creates the folders I need to start a new project, but I need to share it with my team, so instead of telling them to open Automator and set the path (doucments/projects/2020/) by themself. I was thinking that maybe they just can paste it in the 2020 folder, run the app and create the project folders in the same folder.
But I don't know how to set a variable with the current path where my Automator App it's saved. Any ideas? Thanks, guys!
My current workflow
To create a new folder in a specific location of the user's home folder, you can use the the special 'location' variables that automator defines. Click the variables tab button in the upper left corner of the automator window to see the full list of available variables...
So, to create a folder hierarchy like the one shown in the link, but at a standardized location in the documents folder, use a flow like the following:
Since Automator doesn't have a specific variable that gives the location of the created workflow app, if you want a path relative to the app's location you'd use a Run AppleScript action and try to path to me command, but every time I've tried it I've received weird errors which make me think that command doesn't work correctly in Automator. I mean, the following ought to produce the correct result, but it consistently errors out:
Maybe you can make it function...
I'm currently developing a macOS cocoa application. Within one of the windows I'm gathering values from multiple arrays and building an xml file as a key/value backup. After saving the file I'm wanting to set a custom icon for the particular xml file. I've searched throughout stackoverflow and the rest of the web and haven't been able to find any solutions. Throughout the different functions within my application I'm able to pull a previously set icon from a file but haven't been able to set one for a newly created file. Any assistance would be greatly appreciated.
In order to change the XML file icon you should use that:
[[NSWorkspace sharedWorkspace] setIcon:iconImage forFile:path options:0];
Where iconImage is a NSImage of the icon and path is the path of the xml file. Note that the change may not be visible until you rename the file or restart the computer. A workaround is renaming the xml file and then renaming it with the old name back again.
Also, note that macOS icons are different of Windows and Linux icons, so that icon may only be visible in macOS systems (in some cases, only in your machine).
Im building my first app in Xcode using Applescript Objc - I have managed to get it to swap an image (in an outlet called imageViewOne) based on the code below:
-- Declarations
property ImagePath : missing value
property imageViewOne : missing value
property NSImage : class "NSImage"
property parent : class "NSObject"
-- set path as variable
set my ImagePath to "/Users/monkey/XCode/testapp/green.png"
set theImage to initWithContentsOfFile_(POSIX path of ImagePath) of alloc() of class "NSImage" of current application
setImage_(theImage) of imageViewOne
While this works, the problem is that I need to find a way to use a relative path, not "/Users/monkey/XCode/testapp/green.png" (the png is included in the XCode project).
Any help would be well received - as you can see I am new to this!
Would something like this work for you?
set parentDirectory to POSIX path of ((path to me as text) & "::")
AppleScript was originally created for the Classic Mac OS, which did not have a concept of a current directory, as UNIX does. It had "working directories", which are often confused for "current directory" but are quite different.
What you'll need to do (I expect) is find the absolute path of something you can depend on, such as the location of your script, or of an application bundle if your script is inside a bundle, then append your relative path to that absolute path.
In the case of my own iOS App, in Objective-C with Cocoa Touch, I can get my app's Documents folder with:
- (NSString*) documentDirectoryPath
{
return [NSHomeDirectory() stringByAppendingPathComponent: #"Documents"];
}
Can AppleScript call NSHomeDirectory, or does it provide a functional equivalent?
Note that NSHomeDirectory provides the current app's directory, not the user's home.
edit: I don't actually know whether AppleScript provides UNIX-style current directories. Possibly it does. However, a GUI app cannot count on having any specific current directory at the time it launches. Usually it will be the Finder's current directory, for OS X, or for iOS it would be the current directory of the program that presents the app icons and launches them - the program that displays the home screen. I don't know what it's called.
Only Classic Mac OS, as well as Carbon on Mac OS X have the concept of working directories. I don't think Windows has them. UNIX processes can have only one current directory, but Classic Mac OS programs can have as many working directories as memory allows.
Working directories were originally a hack to enable very old - Mac 128k - executables to do file I/O on Heirarchical Filesystem floppy disks. The 1984 Superbowl Mac had "Macintosh Filesystem" floppies that were flat and not heirarchical. That is, one could not have folders within folders.
To be completely clear, I'm discussing working directories because the ORIGINAL AppleScript used them. I really do not know whether AppleScript has been revised to add the concept of the - one per-process - UNIX-style current directory.
Even if AppleScript does provide the current directory, you'll need to set it explicitly, because as I said you cannot count on the current directory at launch being what you expect.
I worked it out.
set ImagePath to current application's NSBundle's mainBundle()'s bundlePath() as text & "/Contents/Resources/imageName.png"
Thanks.
I have the following situation:
I have Mac OS bundle with application which uses some 3rd party dynamic libraries and those libraries depend on some environment variable, let's name it ENV_VAR. I want to set ENV_VAR to some value for my application only because if I set it for the whole system it may breaks some other apps. And it should work transparently to the user i.e. he just run my app from the Application folder by double clicking it. How can I achieve it?
NOTE: dynamic libraries are loaded before main functions starts hence setting this variable in the main doesn't help.
You can add a key "LSEnvironment" to your app bundle's Info.plist. The value can be a dictionary with strings for keys and values and those key-value pairs will be added to the environment when your app is launched by Launch Services (e.g. from the Finder or Dock but not from the Terminal).
<key>LSEnvironment</key>
<dict>
<key>ENV_VAR</key>
<string>value</string>
</dict>
However, in my testing (on Snow Leopard), it was a bit flaky to test, at least when editing the Info.plist of an existing app. Basically, Launch Services caches this part of the app's Info.plist when it first encounters the app and won't necessarily recognize changes on disk. You can sometimes prompt it to reread the Info.plist by, for example, duplicating the app bundle or temporarily moving it to a different folder. Of course, the overkill solution would be to use lsregister to flush and rebuild the cache:
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -seed
This caching issue won't affect your end users, just you as you tweak the Info.plist. Also, it shouldn't affect you if you make the change in your source Info.plist and then build the app using Xcode.
I am not sure if the following works because I don't have such an app to try. The idea is to set the environment variable from the terminal, then call your application:
ENV_VAR=something open -a YourApplication
I'd like to add ~/include to my include path for all projects while using Xcode, something like setting the environment variable CPLUS_INCLUDE_PATH in Linux. (See here for the related Linux question.)
Is this possible in Xcode? I tried setting the above environment variable, but it doesn't seem to work. (And if it is possible, I'd like to also set related paths, like LIBRARY_PATH and LD_LIBRARY_PATH.)
According to Apple's Docs, you are able to provide a default for any build setting using an environment variable.
In this case, you'd want to set HEADER_SEARCH_PATHS. For some reason I don't understand, this doesn't work. It works fine for other build settings (for example, OTHER_CFLAGS), but not for HEADER_SEARCH_PATHS. You can see what the variable name for any setting is by opening the research assistant in the build settings window (book button on the bottom left.)
One var that does work is USER_HEADER_SEARCH_PATHS, which is just like what you want, but only works for paths in double quotes (not in angle brackets.)
So
#include "bar.h"
would work, but
#include <bar.h>
wouldn't.
The build settings plist referenced from the above article on environment variables should end up looking something like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>USER_HEADER_SEARCH_PATHS</key>
<string>~/test</string>
</dict>
</plist>
Hope that helps.
For any of you blessed darlings that are trying to achieve this, or any other hack of ~/.MacOSX/environment.plist, aka global environmental variables in this post-Mountain-Lion world, check out EnvPane - An OS X preference pane for environment variables
EnvPane is a preference pane for Mac OS X 10.8 (Mountain Lion) that lets you set environment variables for all programs in both graphical and terminal sessions. Not only does it restore support for ~/.MacOSX/environment.plist in Mountain Lion, it also publishes your changes to the environment immediately, without the need to log out and back in. This works even for changes made by manually editing ~/.MacOSX/environment.plist, not just changes made via the preference pane.
Add the HEADER_SEARCH_PATHS build setting to your Xcode project. This setting takes a space separated list of paths and if the path ends in a double star (**), it will search subdirectories recursively. So to answer for your example, set HEADER_SEARCH_PATHS to:
~/include
If you had a number of paths to include, you would set it to something like:
~/include ~/my_other_includes/** /usr/local/special_frameworks/**
Just select a .c or .m file in your "Groups & Files Tab" and choose "get Info" through RMB,
then under "build" you can add additional Compiler Flags like -I for additional include searchpath.
Build settings are not environment variables, and environment variables are not build settings. Setting an environment variable will not affect Xcode builds.
USER_HEADER_SEARCH_PATHS is a build setting, and putting a list of paths into it will achieve what you want. If you set the value in a target's inspector, it will take effect for only that target (and only for the Build Configurations you designate). If you set it in the Project inspector, it will take effect in all targets in the project, unless a target overrides it with its own setting.
For paths that are specific to your machine, you should probably define a Source Tree in the Xcode Preferences, such as LOCAL_INCLUDE = ~/include . Then define USER_HEADER_SEARCH_PATHS = $(LOCAL_INCLUDE) in the project. This way, other people can open your project and build it by setting their Source Tree to the particular location of the local includes on their machine, without having to change the project file.