aboutsummaryrefslogtreecommitdiff
path: root/source/Jakobs Tools/AppDelegate.swift
blob: d8ec71e31c959f22b6f36ad4f03ce6a3c9f1ff47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
//  AppDelegate.swift
//  Jakobs Tools
//
//  Created by Jakob Stendahl on 20/02/16.
//  Copyright © 2016 Jakob Stendahl. All rights reserved.
//

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

	@IBOutlet weak var window: NSWindow!

	var statusItem = NSStatusItem?()
	var button = NSStatusBarButton?()
	
	var filesHiden = true
	let appearance = NSUserDefaults.standardUserDefaults().stringForKey("AppleInterfaceStyle") ?? "Light"

	
	func applicationDidFinishLaunching(aNotification: NSNotification) {
		// Insert code here to initialize your app<ication

		let NSVariableStatusItemLength: CGFloat = -1.0;
		
		self.statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(NSVariableStatusItemLength)
		self.button = self.statusItem!.button
		self.button?.title = ""
		self.button?.action = "pressed:"
		self.button?.target = self
		
		let initialState = CheckCurrentState()
		if(initialState.characters.first == "Y") {
			filesHiden = false
			// Initialize app
			setStatusImage("SHOW")
		}
		if(initialState.characters.first == "N") {
			filesHiden = true
			// Initialize app
			setStatusImage("HIDE")
		}
		
	}

	func applicationWillTerminate(aNotification: NSNotification) {
		// Insert code here to tear down your application
	}
	
	func setStatusImage (state : NSString) {
		
		if (appearance == "Dark") {
			if (state == "SHOW") {
				self.button?.image = NSImage(named: "imgShowDark")
			}
			if (state == "HIDE") {
				self.button?.image = NSImage(named: "imgHiddenDark")
			}
		}
		if (appearance == "Light") {
			if (state == "SHOW") {
				self.button?.image = NSImage(named: "imgShow")
			}
			if (state == "HIDE") {
				self.button?.image = NSImage(named: "imgHidden")
			}
		}
	}
	
	func CheckCurrentState() -> String {
		
		// Create a Task instance
		let task = NSTask()
		
		// Set the task parameters
		task.launchPath = "/usr/bin/env"
		task.arguments = ["defaults", "read", "com.apple.finder", "AppleShowAllFiles"]
		
		// Create a Pipe and make the task
		// put all the output there
		let pipe = NSPipe()
		task.standardOutput = pipe
		
		// Launch the task
		task.launch()
		
		// Get the data
		let data = pipe.fileHandleForReading.readDataToEndOfFile()
		let output = NSString(data: data, encoding: NSUTF8StringEncoding)
		
		return(output!) as String
	}
	
	func toggleFilesVisible(state : NSString) {
		
		// Create a Task instance
		let task = NSTask()
		
		// Set the task parameters
		task.launchPath = "/usr/bin/env"
		if(state == "SHOW") {
			task.arguments = ["defaults", "write", "com.apple.finder", "AppleShowAllFiles", "YES"]
			print("SHOW")
		}
		if(state == "HIDE") {
			task.arguments = ["defaults", "write", "com.apple.finder", "AppleShowAllFiles", "NO"]
			print("HIDE")
		}
		
		// Create a Pipe and make the task
		// put all the output there
		let pipe = NSPipe()
		task.standardOutput = pipe
		
		// Launch the task
		task.launch()
		
		// Get the data
		let data = pipe.fileHandleForReading.readDataToEndOfFile()
		let output = NSString(data: data, encoding: NSUTF8StringEncoding)
		
		print("State changed: ")
		print(output!)
		
	}
	
	func refreshFinder() {
		
		// Create a Task instance
		let task = NSTask()
		
		// Set the task parameters
		task.launchPath = "/usr/bin/env"
		task.arguments = ["killall", "Finder", "/System/Library/CoreServices/Finder.app"]
		
		// Create a Pipe and make the task
		// put all the output there
		let pipe = NSPipe()
		task.standardOutput = pipe
		
		// Launch the task
		task.launch()
		
		// Get the data
		let data = pipe.fileHandleForReading.readDataToEndOfFile()
		let output = NSString(data: data, encoding: NSUTF8StringEncoding)
		
		print(output!)
	}
	
	func pressed(sender : AnyObject) {
		
		let currentStateOutput = CheckCurrentState()
		let currentState = currentStateOutput.characters.first
		print(currentState)
		
		if (currentState == "Y") {
			print("YAY")
			filesHiden = false
			toggleFilesVisible("HIDE")
			refreshFinder()
			setStatusImage("HIDE")
		}
		if currentState == "N" {
			print("NAAY")
			filesHiden = true
			toggleFilesVisible("SHOW")
			refreshFinder()
			setStatusImage("SHOW")
		}
		print("POOF")
		
	}
	

}