Thao tác cơ bản này mà không cần thư viện của bên thứ ba. (Được cảnh báo: kiểm tra lỗi tối thiểu.)
// data for 'drag' resource (it's always the same)
#define DRAG_DATA_LENGTH 64
static const unsigned char _dragData[DRAG_DATA_LENGTH]={
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
0x54, 0x45, 0x58, 0x54, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x75, 0x72, 0x6C, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x75, 0x72, 0x6C, 0x6E, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
static void _addData(NSData *data, ResType type, short resId, ResFileRefNum refNum)
{
Handle handle;
if (PtrToHand([data bytes], &handle, [data length])==noErr) {
ResFileRefNum previousRefNum=CurResFile();
UseResFile(refNum);
HLock(handle);
AddResource(handle, type, resId, "\p");
HUnlock(handle);
UseResFile(previousRefNum);
}
}
void WeblocCreateFile(NSString *location, NSString *name, NSURL *fileUrl)
{
NSString *contents=[NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
@"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
@"<plist version=\"1.0\">\n"
@"<dict>\n"
@"<key>URL</key>\n"
@"<string>%@</string>\n"
@"</dict>\n"
@"</plist>\n", location];
if ([[contents dataUsingEncoding:NSUTF8StringEncoding] writeToURL:fileUrl options:NSDataWritingAtomic error:nil])
{
// split into parent and filename parts
NSString *parentPath=[[fileUrl URLByDeletingLastPathComponent] path];
NSString *fileName=[fileUrl lastPathComponent];
FSRef parentRef;
if(FSPathMakeRef((const UInt8 *)[parentPath fileSystemRepresentation], &parentRef, NULL)==noErr)
{
unichar fileNameBuffer[[fileName length]];
[fileName getCharacters:fileNameBuffer];
FSCreateResFile(&parentRef, [fileName length], fileNameBuffer, 0, NULL, NULL, NULL);
if (ResError()==noErr)
{
FSRef fileRef;
if(FSPathMakeRef((const UInt8 *)[[fileUrl path] fileSystemRepresentation], &fileRef, NULL)==noErr)
{
ResFileRefNum resFileReference = FSOpenResFile(&fileRef, fsWrPerm);
if (resFileReference>0 && ResError()==noErr)
{
_addData([NSData dataWithBytes:_dragData length:DRAG_DATA_LENGTH], 'drag', 128, resFileReference);
_addData([location dataUsingEncoding:NSUTF8StringEncoding], 'url ', 256, resFileReference);
_addData([location dataUsingEncoding:NSUTF8StringEncoding], 'TEXT', 256, resFileReference);
_addData([name dataUsingEncoding:NSUTF8StringEncoding], 'urln', 256, resFileReference);
CloseResFile(resFileReference);
}
}
}
}
}
}
Có ai có thể xác nhận tệp webloc chỉ với một ngã ba dữ liệu hoạt động với Safari không? Tôi đã cố gắng lưu ví dụ XML plist của Schwa như một tệp tin webloc nhưng khi kéo nó vào Safari nó chỉ mở một cửa sổ trống. Tôi cũng đã thử lưu một tệp webloc với Firefox. Thật thú vị, Firefox viết một tập tin với cả một ngã ba dữ liệu (XML plist như trong ví dụ của Schwa) và một ngã ba tài nguyên. Kéo tệp đó vào Safari hoạt động. Nhưng khi tôi đã cố gắng loại bỏ các ngã ba tài nguyên (bằng cách ghi đè nó trên dòng lệnh với một dòng sản phẩm nào) nó không còn làm việc. Thử nghiệm được thực hiện bằng cách sử dụng Safari 4.0.5 và Firefox 3.6.3. – Rinzwind
Làm rõ thêm cho nhận xét trước đó của tôi: Tôi đã không thử chỉ đơn giản là nhấp đúp vào tệp để mở tệp. Tôi vừa làm và DOES đó làm việc với tệp webloc-data-fork-only, nó sẽ mở URL trong Safari. Nhưng như tôi đã nói ở trên, DRAGGING tệp webloc vào Safari KHÔNG hoạt động, nó sẽ trả về một cửa sổ trống. (Ngoài ra, kéo tệp vào cửa sổ dấu trang Safari tạo dấu trang có URL: // URL cho tệp webloc, thay vì dấu trang cho URL chứa trong tệp webloc.) – Rinzwind