(half solve)Why copy file reference to clipboard programmatically only working for system folder in MacOS? - swift

Solution
add this code pasteboard.data(forType: .fileURL) after pasteboard.writeObjects(emptyArray), it will work.
But I don't know why?
System Verison: MacOS Ventura 13.0
copy.swift
#!/usr/bin/swift
import Cocoa
private func copyToClipBoard(path :String) {
let pasteboard = NSPasteboard.general
var emptyArray = [NSPasteboardWriting]()
emptyArray.append(NSURL(fileURLWithPath: path))
pasteboard.clearContents()
pasteboard.writeObjects(emptyArray)
}
copyToClipBoard(path: CommandLine.arguments[1])
If I run swift copy.swift /Users/USER/Downloads/FILE, paste to chat window for third-party applications is working.
If I run swift copy.swift /Users/USER/tmp/FILE, cannot paste to chat window for third-party applications, application occur no permission error, I already give full access disk to this application.
What's the difference between Downloads and tmp?
How can I make copy file reference programmatically working for any folder?

Related

Set custom icon for file on Mac using Swift

In my application, I want to set a custom icon for a folder.
I am using the following code.
let path = "/Users/customUser/Desktop/test"
let icon = NSWorkspace.shared.icon(forFile: "/Applications/Safari.app")
let res = NSWorkspace.shared.setIcon(icon, forFile: path, options: [])
print(res)
But it returns false
What am I doing wrong?
Or is there another way to change the icon of a file or folder?
Mac apps are sandboxed by default, and only have write access to a small set of directories in their sandboxed. You are probably failing due to your app not having permission.
As I recall, if you have the user select the file with a file save dialog, that implicitly grants the app permission to write to the file.

why in xcode, home directory is showing as /Users/myname/Library/Containers/... and not the main folder?

I searched a lot and couldn't find the answer.
I just started using xcode and have a very very simple program named "Image Test".
---> It has a button, and a textField. I want to show my system home directory path in the textField. This is my code:
#IBOutlet weak var textField1: NSTextField!
#IBAction func Button1(_ sender: Any) {
do{
let home = try FileManager.default.homeDirectoryForCurrentUser
textField1.stringValue = home.path
}
catch{}
}
it works but the returned path is like:
/Users/myname/Library/Containers/com.Image-Test/Data
I have tested this code before in Xcode Playground and it was correct. Its the same code but not throwing the right path.
It's because of sandboxing. The documentation states:
The App Sandbox Container Directory
It is located at a system-defined path, within the user’s home directory. In a sandboxed app, this path is returned when your app calls the NSHomeDirectory function.
You can verify this by removing the App Sandbox capability (note: I am not suggesting that this is what you should do for your app).
PS: If you intend to submit your app to the AppStore, sandboxing must be enabled.

MusicSequenceFileLoad Works in Playgrounds but not in Project

what am i doing wrong?
i'm trying to open a midi file and read it's contents. so far i can open the file in playgrounds but i get "Open::fopen failed" in my project
override func viewDidLoad() {
super.viewDidLoad()
let path = NSURL.fileURL(withPath: "/users/me/file.mid")
var midfile: MusicSequence?
NewMusicSequence(&midfile)
MusicSequenceFileLoad(midfile!, path as CFURL, .midiType , .smf_ChannelsToTracks)
}
i do have import AudioToolbox and CoreMIDI
EDIT: some progress
I'm able to open midi files from bundle with CFBundleCopyResourceURL, but i want to be able to open files from anywhere in finder
fileManager also fails, it returns nil when used inside a project and works in playgrounds.
figured it out.
code fails because the app runs sandboxed and the NSURL can't be accessed

What would cause Xcode MacOs app to point main bundle at "/Applications/Xcode.app"?

I'm struggling to write my first Swift3 app - a screensaver because I can't reference any local files. After hours of debugging and setting up a Playground I found that the Bundle.main.bundlePath is not pointing at my app but rather "/Applications/Xcode.app". Which explains why in the code below - video.mp4 is never found but I get nil back from selecting the Bundle by identifier as well.
Any suggestions on how to fix this? My one local file (video.mp4) is readable, copied as a Bundle Resource and has no spurious xattrs. I'm stuck! Any help appreciated.
Running 10.12.3/Xcode 8.2.1, Swift 3.0.2
Playground code below:
import Cocoa
import PlaygroundSupport
import AVKit
import AVFoundation
var frameRect = NSRect(x: 0, y: 0, width: 600, height: 600)
var mainView = NSView(frame: frameRect)
let bundle = Bundle(identifier: "com.modprods.koorigras-test.Playground")
debugPrint(bundle)
debugPrint(Bundle.main.bundlePath)
guard let path = Bundle.main.path(forResource: "video",ofType:"mp4") else {
debugPrint("not found")
exit(1)
}
As per Eric's comment, this is correct behavior for a Playground.
For other n00bs, creating a new Playground inside your workspace results in greyed out Sources and Resources folders that if you try to show in finder nothing happens (because they don't exist). To access local files from within a new Playground
create the Resources subfolder
copy the asset to Resource
make sure Copy Bundle Resources includes this asset for the target
It appears there is no way of accessing other bundles from within a Playground.

Where should I add my own Markdown file in Vapor project?

I want to add my own Markdown file, a file named profile.md. However, I don't know where I should add the file in my Vapor project, which is developed in Xcode.
I tried adding it to Sources/ or just the root directory, but it cannot be searched by Bundle.main.url(forResource:) function, like:
guard let url = Bundle.main.url(forResource: "profile", withExtension: ".md") else { ... }
Also, I found that I already added it under Copy Bundle Resources tab.
In the normal app (for iOS and macOS), the function above works properly.
How can I add my own file to be used in Vapor project developed in Xcode?
This is a very basic example of loading the contents of a file from a custom location using Foundation rather than Vapor's template view loading mechanism, and returning it as a text response.
drop.get { req in
let templateString = try String(contentsOfFile: drop.workDir + "Resources/profile.md")
return templateString
}
I'm not sure exactly what you mean by "parse, and embed it into another template file" but you'd put that bit in between the lines above.