Mac Objective-C convert HFS style paths to POSIX path [duplicate] - objective-c

This question already has answers here:
How to convert a classic HFS path into a POSIX path
(2 answers)
Closed 3 years ago.
While trying the approach suggested here, working directory gets prefixed instead of path conversion. Leading : is HFS path does not make any difference.
NSString * ttt = #"Macintosh HD:Users:gautam:code:Help:";
if (CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)ttt, kCFURLPOSIXPathStyle, false))
{
NSString * posixPath = [(__bridge NSURL*)url path];
// posixPath __NSCFString * "/Users/gautamjha/code/Macintosh HD:Users:gautam:code:Help:"
const char * secondName = [posixPath UTF8String];
// above does not help either working directory gets prefixed.
}

The error occurs because you are passing the source type kCFURLPOSIXPathStyle, but you want to create the URL from an HFS path.
As mentioned in my linked answer the constant kCFURLHFSPathStyle is unavailable, you have to replace it with the raw value 1.
if (CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)ttt, 1, false))

Related

as for 'replace' usage, does it support to replace the placeholder with value in place (I mean "over-write"))?

I need to upload a file to the server side, however, before doing that I need to replace a placeholder in the file with a dynamic value. does it support to get the placeholder replaced in place dynamically?
I noticed that I can replace the placeholder with ease using the 'replace' keyword. The following is my script:
Given path 'common/upload'
And multipart fields read('classpath:mainFlow/labresultUpload.json')
* def filename = 'PKU.A22backup'
* def someString = read('PKU.A22backup')
* print someString
* replace someString
|token|value|
|labsampleid|'123456'|
* print someString
* multipart file file = { read: "#(filename)", filename: "#(filename)"}
When method post
Then status 200
* def result = response[0].result
However, I need to replace the placeholder in place (here, I mean over-write) dynamically and then upload the file to the server side.
You have the option to supply a value instead of the file-name: https://github.com/intuit/karate#multipart-file
* multipart file file = { value: "#(someString)", filename: "#(filename)" }

ASSERT failure in QList<T>::at: "index out of range" in Qt 5

I want to generate *.png files from my directory randomly in qt using qrand() function.
to do that I have created a QStringList object so that I can store all files I have in my
directory.thinking that I have some list of files in my QStringList object when I run my application it crush.
I have also warning in my issue pane:> warning: unknown escape sequence: '\D' [enabled by default]
applicationPath = "C:\\Users\\Tekme\Documents\\QtProject\\4Toddler";
^
the reason I add the above warning issues is that, I am on window machine so I am thinking the problem can be path problem
the code is
QString MainWindow::randomIcon()
{
QStringList iconFileList;
QString searchPath = applicationPath + "\\icons";//applicationPath =
QDir directory = QDir(searchPath);
QStringList filters;
filters << "*.png";
directory.setNameFilters(filters);
iconFileList = directory.entryList(QDir::AllEntries);//i have 6 *.png files
int randomIndex = qrand() % iconFileList.count();
return iconFileList.at(randomIndex);//my application crash here
}
Even when I try to replace iconFileList.at(randomIndex) by iconFileList.at(2) it crush.
I am sure I have more than 2 files in my directory
The answer is in the error:
applicationPath = "C:\\Users\\Tekme\Documents\\QtProject\\4Toddler";
Should be
applicationPath = "C:\\Users\\Tekme\\Documents\\QtProject\\4Toddler";
You are missing an extra slash

Can I use my own filename when using NSTemporaryDirectory

I am generating a PDF file and saving it to my device before e-mailing it from within my application. At the moment it does not get cleared down either manually or automatically so I was looking at NSTemporaryDirectory. I have looked at various sites and also on here for the answer to my specific query but cannot find it.
I have the following function:
+(NSString *)getTempFilePathName {
NSString *tempFilePathName = nil;
NSString *tempFileTemplate = [NSTemporaryDirectory() stringByAppendingPathComponent:#"tempfile.XXXXXX"];
const char *tempFileTemplateCString = [tempFileTemplate fileSystemRepresentation];
char *tempFileNameCString = (char *)malloc(strlen(tempFileTemplateCString) + 1);
strcpy(tempFileNameCString, tempFileTemplateCString);
int fileDescriptor = mkstemp(tempFileNameCString);
if (fileDescriptor != -1) {
// File opened successfully
tempFilePathName = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:tempFileNameCString length:strlen(tempFileNameCString)];
close(fileDescriptor);
}
free(tempFileNameCString);
return tempFilePathName;
}
This returns me an NSString with the full path and filename to a temp file ending with the 'XXXXXX' component of the filename replaced with unique combo of letters and numbers. As I am using this to save a PDF file, I need to fix the 'XXXXXX' bit with the extension ".PDF". Preferably I'd like to also specify the filename itself too so something like "Order-123.PDF".
Can I just edit my method to pass in a filename and use that in the stringByAppendingPathComponent parameter? Is the directory name generated unique per call to this method?
I have solved this by myself. After the line that calls the above routine I do:
NSString *newFilePath = [tempNameStub stringByAppendingPathExtension:#"pdf"];
Which adds .pdf to the end.

How to set artwork to an iTunes song using NSApplescript in Xcode?

there are some similars questions here but none involves NSApplescript.
I'm trying to add iTunes artwork to a selected track, my code is:
set newFile to add aFileName as POSIX file to ipod_lib
set current_track to newFile
set jpegFilename to ":temp:artwork.jpg" as string
set data of artwork 1 of current_track to (read (file jpegFilename) as picture)
tell current_track
set name to "Song Name" as string
set artist to "Custom Artist" as string
set album to "Custom Album" as string
set year to "2011" as string
set track number to "1" as number
set track count to "38" as number
end tell
Where aFileName is the path of an mp3.
I'm testing this using Script Debugger 4.5 and it works fine, but when I copy the code to my Xcode project and run it wont set the artwork neither the other meta data.
But if I comment the "set data of artwork.." line, then it does set the other meta data (name, artist, etc)
My Xcode code is:
NSString *scriptote = #"with timeout of 600 seconds\n"
"tell application \"iTunes\"\n"
...
"set newFile to add aFileName as POSIX file to ipod_lib\n"
"set current_track to newFile\n"
"set jpegFilename to \":temp:artwork.jpg\" as string\n"
// If a comment the following line, everything else works, if I left this line, no meta data is set.
"set data of artwork 1 of current_track to (read (file jpegFilename) as picture)\n"
"tell current_track\n"
"set name to \"Song Name\" as string\n"
"set artist to \"Custom Artist\" as string\n"
"set album to \"Custom Album\" as string\n"
"set year to \"2011\" as string\n"
"set track number to \"1\" as number\n"
"set track count to \"38\" as number\n"
"end tell\n"
...
;
if ((ascript = [[NSAppleScript alloc] initWithSource:scriptote])) {
status = [[ascript executeAndReturnError:&errorInfo] stringValue];
if (!errorInfo) {
// do something
NSLog(#"NO Error");
} else {
// process errors
// NSRange errorRange = [[errorInfo objectForKey:#"NSAppleScriptErrorRange"] rangeValue];
NSLog(#"Error\n Error line: ");
}
[ascript release];
}
I have been searching on google for many hours without luck.
I don't want to use ScriptBridge framework because I will need to translate lot of applescripts so its not an option for me right now.
Any help will be greatly appreciated.
Found the problem, in case someone else reach here in the future the problem was with the HFS path format style.
I was getting the path to my artwork using NSSearchPathForDirectoriesInDomains and then replacing all the #"/" for #":" to format the path in HFS style.
The problem is that HFS path format style need a full path including Volume Name and NSSearchPathForDirectoriesInDomain and any other NSFileManager methods return paths starting in /Users/.... and I needed a path like this: Macintosh HD/Users/...
To get the HFS full path I used the following code:
// Get an HFS-style reference to a specified file
// (imagePath is an NSString * containing a POSIX-style path to the artwork image file)
NSURL *fileURL = [NSURL fileURLWithPath:imagePath];
NSString *pathFormatted = (NSString *)CFURLCopyFileSystemPath((CFURLRef)fileURL, kCFURLHFSPathStyle);
That solves the problem, use the pathFormatted string to set the data of artwork and it should work.
Hope this would help someone someday -)

How to convert file name with path to short file name (DOS style) in Adobe AIR?

How to convert file name with path to short file name (DOS style) in Adobe AIR?
For example convert next path
"C:\Program Files\Common Files\Adobe AIR\Versions\1.0\Resources\Adobe AIR Updater.exe"
to
"C:\PROGRA~1\COMMON~1\ADOBEA~1\VERSIONS\1.0\RESOUR~1\ADOBEA~1.EXE"
Is there any algorithm?
Assuming your text portion is a string variable, you can split it by using "\" as delimiter. Then, you will have an array which you can use to check if each block is longer than 8 characters. While looping the array you can chop the last characters of each long block and put ~1. Since you're in the loop, you can progressively add to a temporary variable all these changes which will give you the final edited result at the end.
The only part that's a bit tricky is to pay attention to .exe part at the end.
So, if I were you, I'd start reading on String.split(), String.substring(), for loop, arrays
Here's my handy method that does this below:
public static string GetShortPathName(string path)
{
string[] arrPath = path.Split(System.IO.Path.DirectorySeparatorChar);
path = arrPath[0]; // drive
// skip first, ( drive ) and last program name
for (int i = 1; i < arrPath.Length - 1; i++)
{
string dosDirName = arrPath[i];
if (dosDirName.Count() > 8)
{
dosDirName = dosDirName.Substring(0, 6) + "~1";
}
path += System.IO.Path.DirectorySeparatorChar + dosDirName;
}
// include program name if any
path += System.IO.Path.DirectorySeparatorChar + arrPath[arrPath.Length - 1];
return path;
}