How can I execute a simple Applescript from a C++ program? - c++

I would like to execute the Applescript command tell application "Finder" to open POSIX file */path/to/somefilename* from a C++ program. It looks like I might want to use OSACompileExecute, but I haven't been able to find an example of how to use it. I keep finding examples of how to use the OSACompile Terminal command. Can someone provide an example or a link to an example?

Ok, the trick was to not bother trying to compile and execute the Applescript but to simply use the osascript system command:
sprintf(cmd, "osascript -e 'tell app \"Finder\" to open POSIX file \"%s/%s\"'", getcwd(path, MAXPATHLEN), file);
system(cmd);
path and file are both char[] variables.
I got the clue from this excerpt from Applescript: The Definitive Guide.

Here's an example C function for reading a Get Info comment from the finder using AppleScript.
You could modify it for what you want.
NSString * readFinderCommentsForFile(NSString * theFile){
/* Need to use AppleScript to read or write Finder Get Info Comments */
/* Convert POSIX file path to hfs path */
NSURL * urlWithPOSIXPath = [NSURL fileURLWithPath:theFile];
NSString * hfsStylePathString =
(__bridge_transfer NSString *)CFURLCopyFileSystemPath((__bridge CFURLRef) urlWithPOSIXPath, kCFURLHFSPathStyle);
/* Build an AppleScript string */
NSString *appleScriptString = #"tell application \"Finder\"\r get comment of file ";
appleScriptString = [appleScriptString stringByAppendingString:#"\""];
appleScriptString = [appleScriptString stringByAppendingString:hfsStylePathString];
appleScriptString = [appleScriptString stringByAppendingString:#"\""];
appleScriptString = [appleScriptString stringByAppendingString:#"\r end tell\r"];
NSString *finderComment;
NSAppleScript *theScript = [[NSAppleScript alloc] initWithSource:appleScriptString];
NSDictionary *theError = nil;
finderComment = [[theScript executeAndReturnError: &theError] stringValue];
NSLog(#"Finder comment is %#.\n", finderComment);
return finderComment;

Related

How to write and log to a file on my NAO robot

I'm using Choregraphe on Windows to implement programms with Python for my NAO robot. I have two problems which I can't solve by myself:
I want to create a textfile on NAO robot and write information in it. Later I want to store it to my computer. Leading to this article - reading a textfile
I used the following code in a Python Box:
import logging
filepath = os.path.join(os.path.dirname(ALFrameManager.getBehaviorPath(self.behaviorId)), "fileName.txt")
maybeContains = None
try:
with open(filepath, "r") as textfile:
maybeContains = textfile.readlines()
except:
pass
with open(filepath, "a") as textfile:
if maybeContains == "":
agenda = "type1;type2;\n"
textfile.write(agenda)
textfile.write(storedData)
else:
textfile.write(storedData)
self.onStopped()
When I try to download the file "fileName.txt" via Connection > Advanced > File Transfer there isn't that file in one of those listed orders.
I also want to create a textfile on the robot to log informations from the coding, so I can check the actions of the robot. As in 1. I want to download the log fiel to the computer.
I added to the onLoad() method of a "Say Text"-box the following code:
def onLoad(self):
self.logging.basicConfig(filename="20180712.log", format='%(asctime)s %(levelname)s-8s [%(filename)s:%(lineno)d]%(message)s', level=logging.DEBUG)
self.logger = self.logging.getLogger("Behavior - Box") `
Before a command, which should be logged I call
` self.logger("what happened here")
the "Connection > Advanced > File Transfer" open a file at a specific location.
Depending on the version of your robot. Some years ago it was "/var/www" or "~/ftp/" ...
On my current NAO (2.1), it's in "/home/nao".
So the good way in my case is to create the file at this place:
filepath = "/home/nao/myfile.txt"
That being said, there's better way to get a file from your robot, on windows, you can use winscp (gui) or pscp (cli), it's far more convenient than Choregraphe...
Good luck.

How to get a file to be used as input of the program that ends with special character in python

I have an output file from a code which its name will ends to "_x.txt" and I want to connect two codes which second code will use this file as an input and will add more data into it. Finally, it will ends into "blabla_x_f.txt"
I am trying to work it out as below, but seems it is not correct and I could not solve it. Please help:
inf = str(raw_input(*+"_x.txt"))
with open(inf+'_x.txt') as fin, open(inf+'_x_f.txt','w') as fout:
....(other operations)
The main problem is that the "blabla" part of the file could change to any thing every time and will be random strings, so the code needs to be flexible and just search for whatever ends with "_x.txt".
Have a look at Python's glob module:
import glob
files = glob.glob('*_x.txt')
gives you a list of all files ending in _x.txt. Continue with
for path in files:
newpath = path[:-4] + '_f.txt'
with open(path) as in:
with open(newpath, 'w') as out:
# do something

How to read a XML file using fstream in IOS

I am using XCode, and here is a c++ code in XCode
std::fstream stream("templates.Xml", std::ios::binary | std::ios::in);
if(!stream) return false;
I put the Xml file in the folder that contain the ".xcodeproj", and I put it in the folder that contains ".app" but the stream always return false, why?
It looks like you are trying to open the file from the wrong directory.
"templates.Xml" is saved in the bundle- is not saved in the documents directory. By default, if you open "./filename", this actually points to:
/Users/arinmorf/Library/Application Support/iPhone Simulator/7.0.3/Applications/246E91F9-FAB2-4A46-B1F1-855B5363F24D/Documents/
Where arinmorf would be your username and the long hex string is randomly generated every time you install the app on the simulator.
The templates.xml file would be found in:
/Users/arinmorf/Library/Application Support/iPhone Simulator/7.0.3/Applications/246E91F9-FAB2-4A46-B1F1-855B5363F24D/iFly.app/templates.xml
iFly.app is the name of my app, yours would be "T". BUT you can't use the absolute path in your project, because of the randomly generated string, you need to use the NSBundle or CFBundleRef.
In objective-C, you would use:
filePath = [[NSBundle mainBundle] pathForResource:#"templates" ofType:#"xml"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
In C++ it looks like:
CFURLRef fileURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("templates"), CFSTR("xml"), NULL);
CFStringRef filePath = CFURLCopyFileSystemPath(fileURL, kCFURLPOSIXPathStyle);
CFStringEncoding encodingMethod = CFStringGetSystemEncoding();
const char *path = CFStringGetCStringPtr(filePath, encodingMethod);
FILE* f = fopen(path, "r");
(Credit to Chris Frederick at https://stackoverflow.com/a/8768366/2070758 for C++ version)
Yes, I solved the problem in two ways either depending on Main Bundle, or create custom bundle and use it.

TCL C API Create and Register new Channel

I use Tcl 8.6(windows) and i can't register and use new channels
std::ofstream file("1.txt");
Tcl_Channel kanal = Tcl_CreateChannel(Type, "myChann", file, TCL_WRITABLE);
Tcl_RegisterChannel(interp, kanal);
file.close();
and the Type is
Tcl_ChannelType* Type = new Tcl_ChannelType();
Type->closeProc = closeProc;
Type->inputProc = inputProc;
Type->outputProc = outputProc;
Type->typeName = "My own chann";
Type->version = TCL_CHANNEL_VERSION_2;
functions are simply, they have only std::cout
i run interpreter with script
"chan puts myChan whatever"
and nothing happend, no errors from interpreter, no output(console, file).
i dont know how to bite this,
this is first goal, create new chanel and use it,
the second is to replace TCL_STDOUT with my own channel(it can be std::ofstream),
so that when i run interpreter with
"puts WhatEver"
that string go to the std::ofstream
Probably you did not flush the output.
This can be done with
flush myChan
You can also configure your channel to flush at the end of line or whenever anything is written to it:
chan configure myChan -buffering line
(or none). stdout is by default configured with -buffering line

Boost-Python: Load python module with unicode chars in path

I'm working on game project. I use python 2.7.2 for scripting. My application works fine with non unicode path to .exe. But it can't load scripts with unicode path using
boost::python::import (import_path.c_str());
I tried this example
5.3. Pure Embedding http://docs.python.org/extending/embedding.html#embedding-python-in-c
It also can't handle unicode path. I linked python as dll.
Explain me, please, how to handle such path.
boost::python::import needs a std::string, so chances are that import_path misses some characters.
Do you have to work on multiple platform ? On Windows, you could call GetShortPathName to retreive the 8.3 filename and use that to load your dll.
You can make a quick test :
Rename your extension to "JaiDéjàTestéÇaEtJaiDétestéÇa.pyd".
At the command line, type dir /x *.pyd to get the short file name (JAIDJT~1.PYD on my computer)
Use the short name to load your extension.
+The file name above if French for "I already tested this and I didn't like it". It is a rhyme that takes the edge off working with Unicode ;)
This isn't really an answer that will suit your needs, but maybe it will give you something to go on.
I ran into a very similar problem with Python, in my case my application is a pure Python application. I noticed as well that if my application was installed to a directory with a path string that could not be encoded in MBCS (what Python converts to internally for imports, at least Python prior to 3.2 as far as I understand), the Python interpreter would fail, claiming not module of that name existed.
What I had to do was write an Import Hook to trick it into loading those files anyway.
Here's what I came up with:
import imp, os, sys
class UnicodeImporter(object):
def find_module(self,fullname,path=None):
if isinstance(fullname,unicode):
fullname = fullname.replace(u'.',u'\\')
exts = (u'.pyc',u'.pyo',u'.py')
else:
fullname = fullname.replace('.','\\')
exts = ('.pyc','.pyo','.py')
if os.path.exists(fullname) and os.path.isdir(fullname):
return self
for ext in exts:
if os.path.exists(fullname+ext):
return self
def load_module(self,fullname):
if fullname in sys.modules:
return sys.modules[fullname]
else:
sys.modules[fullname] = imp.new_module(fullname)
if isinstance(fullname,unicode):
filename = fullname.replace(u'.',u'\\')
ext = u'.py'
initfile = u'__init__'
else:
filename = fullname.replace('.','\\')
ext = '.py'
initfile = '__init__'
if os.path.exists(filename+ext):
try:
with open(filename+ext,'U') as fp:
mod = imp.load_source(fullname,filename+ext,fp)
sys.modules[fullname] = mod
mod.__loader__ = self
return mod
except:
print 'fail', filename+ext
raise
mod = sys.modules[fullname]
mod.__loader__ = self
mod.__file__ = os.path.join(os.getcwd(),filename)
mod.__path__ = [filename]
#init file
initfile = os.path.join(filename,initfile+ext)
if os.path.exists(initfile):
with open(initfile,'U') as fp:
code = fp.read()
exec code in mod.__dict__
return mod
sys.meta_path = [UnicodeImporter()]
I still run into two issues when using this:
Double clicking on the launcher file (a .pyw file) in windows explorer does not work when the application is installed in a trouble directory. I believe this has to do with how Windows file associations passes the arguments to pythonw.exe (my guess is Windows passes the full path string, which includes the non-encodeable character, as the argument to the exe). If I create a batch file and have the batch file call the Python executable with just the file name of my launcher, and ensure it's launched from the same directory, it launches fine. Again, I'm betting this is because now I can use a relative path as the argument for python.exe, and avoid those trouble characters in the path.
Packaging my application using py2exe, the resulting exe will not run if placed in one of these trouble paths. I think this has to do with the zipimporter module, which unfortunately is a compiled Python module so I cannot easily modify it (I would have to recompile, etc etc).